perlperl
asked on
erasing element of list whle iterating
The list I have contains list of dynamic object
Am I leaking here or creating infinite loop ?
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;
}
}
Am I leaking here or creating infinite loop ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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...
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...
ASKER