Solved

erasing element of list whle iterating

Posted on 2013-10-27
4
298 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
ID: 39604372
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
ID: 39604430
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
ID: 39604431
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
ID: 39604493
>> 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

825 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