Solved

STL list -- Access Violation on remove() -- WHY?

Posted on 2000-04-20
10
391 Views
Last Modified: 2013-12-14
This is a contrived example from my real program.  I am new
to STL.

Why does this give me an Access Violation in the final
for() loop?  Is there some sort of reset that I am supposed
to call between the loops?  

Later, I want to remove specific entries from the list, display
the list, and when the program exits, I want to prevent the memory leak and delete all the remaining entries.  





// stllisttest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iomanip.h>
#include <list>
using namespace std;

class CData {
public:
      int x;
      int y;
};

typedef list<CData*> DATALIST;

int main(int argc, char* argv[])
{
      CData* pData;
      DATALIST dataList;
      DATALIST::iterator i;
      int n;

      for (n = 0; n < 10; n++)
      {
            pData = new CData();
            pData->x = n;
            pData->y = n * n;;

            dataList.insert(dataList.end(), pData);
      }
      
      for (i = dataList.begin(); i != dataList.end(); i++)
      {
            CData* pData = *i;
            cout << "x=" << pData->x << ", y=" << pData->y << endl;
      }

      for (i = dataList.begin(); i != dataList.end(); i++)
      {
            CData* pData = *i; // why does this fail with Access Violation on this line?
            dataList.remove(pData);
            delete pData;
      }

      return 0;
}

0
Comment
Question by:alfredj
  • 5
  • 3
  • 2
10 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Your problem seems to be the call to 'list::remove()':

"list::remove
void remove(const T& x);
The member function removes from the controlled sequence all elements, designated by the iterator P, for which *P == x."

If you want to remove a single list element, use 'list::erase()' instead, e.g.


for (i = dataList.begin(); i != dataList.end(); i++)
{
CData* pData = *i;
dataList.erase(pData);
delete pData;
}



0
 

Author Comment

by:alfredj
Comment Utility
Okay, so I guess I do not understand the documentation.

Does remove() remove EVERYTHING IN THE LIST?  What does "removes from the controlled sequence all elements, designated by the iterator P, for which *P == x" mean (in English? German?)
Thanks, if this works, I'll happily give the points.
0
 

Author Comment

by:alfredj
Comment Utility
BTW, the line...

dataList.erase(pData);

....does not compile, so I changed it to...

dataList.erase(i);

....and that compiles, but fails on the second iteration thru the loop instead of the first.

0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Is' schwer zu sagen, ich hätte auch erst 'alle' verstanden ;-)
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Well, to be honest, I'd usually use a construct like that:

for (i = dataList.begin(); i != dataList.end(); i++)
{
delete (*i);
}

dataList.clear();
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:alfredj
Comment Utility
Okay, that's cool, but I do need to
delete a single item in the list (as a result of the user's action) then write the remaining data to a database and delete the remaining items in the list (to prevent the memory leak).

0
 

Author Comment

by:alfredj
Comment Utility
So I added a loop like this, but it fails on the second iteration.  Is it illegal to change the list when you are iterating thru it?

-Alfred


      for (i = dataList.begin(); i != dataList.end(); i++)
      {
            CData* pData = *i; // fails here on the second iteration
            if (pData->x == 4)
            {
                  cout << "DELETING: x=" << pData->x << ", y=" << pData->y << endl;
                  dataList.erase(i);
                  //delete (*i);
            }
      }
0
 
LVL 3

Accepted Solution

by:
GlennDean earned 100 total points
Comment Utility
Hi alfredj:
   You are removing an element, then trying to use the iterator to move to the next (but the iterator points to a non-existing element.
for (i = dataList.begin(); i != dataList.end(); )
{
CData* pData = *i;
i++; //move to an existing element
dataList.remove(pData);
delete pData;
}
   Glenn


0
 

Author Comment

by:alfredj
Comment Utility
Okay, this works.  Explain the difference between .remove() and .erase() and you get an 'A'

0
 
LVL 3

Expert Comment

by:GlennDean
Comment Utility
erase deletes a particular element while remove deletes all the elements equal to a particular value.  In your case, each time you called remove it deleted one item because only 1 element had a value identical to the pointer.
   Glenn
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now