Solved

erasing element of list whle iterating

Posted on 2013-10-27
4
296 Views
Last Modified: 2013-10-27
The list I have contains list of dynamic object

typedef	std::list<MyClass*> ace_list_type;
ace_list_type obj; // List of Dynamically allocated object 

for (ace_list_type::iterator it = obj.begin(); it != obj.end();)
{
     if (some_condition)  {
	    delete (*it);         // Free's the Memory
	    it = obj.erase(it); // Remove from list 
     } else {
          ++it;
     }
}

Open in new window


Am I leaking here or creating infinite loop ?
0
Comment
Question by:perlperl
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
Comment Utility
No, all is fine. As a side note, this e.g. would not work with a vector, because the 'erase()' would invalidate the iterator, but with a list, everything is fine. Also, try to avoid storing poinbters in containers, but I assume you have a good reason to do that.
0
 

Author Comment

by:perlperl
Comment Utility
Thanks. I agree but its some legacy code. As longs as memory is freed it should be fine ;) to store pointers in containers.
0
 

Author Comment

by:perlperl
Comment Utility
Jkr, you never responded to my earlier Q. I need to close that Q but now sure which of your comments corresponds to my last comment ;)
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>> As longs as memory is freed it should be fine

Agreed. Yet that's the risk - when more than one developer works on such code, chances are that misunderstandings happen. In general, that approach is harder to maintain. But sometimes, it's hard to do things differently...
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

744 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

18 Experts available now in Live!

Get 1:1 Help Now