Solved

erasing element of list whle iterating

Posted on 2013-10-27
4
300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

726 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