Link to home
Start Free TrialLog in
Avatar of perlperl
perlperl

asked on

erasing element of list whle iterating

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 ?
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of perlperl
perlperl

ASKER

Thanks. I agree but its some legacy code. As longs as memory is freed it should be fine ;) to store pointers in containers.
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 ;)
>> 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...