Wednesday, 13 June 2007
Memory Leaks with MFC CPtrArray
Just finished debugging some 3rd party code with memory leaks.
The code used the RemovalAll member operation on the MFC CPtrArray class. This operation needs to be used with care as all the array elements need to be deleted AFTER the call to RemoveAll. Calling RemoveAll before deleting all the array objects will cause a memory leak.
Another memory leak was caused by deleting a class pointer that had been passed to a function as a void pointer. When delete is executed on a void pointer the destructor of the class is obviously not called.
One final comment on memory leaks fixing is beware when using the EVC4 debug environment. Over the past few days I was chasing a memory leak that did not exist but was a symptom of using the debugger!
Subscribe to:
Post Comments (Atom)
2 comments:
This operation needs to be used with care as all the array elements need to be deleted AFTER the call to RemoveAll.
Calling RemoveAll before deleting all the array objects will cause a memory leak.
dont u think these two are contradicting statements.
I think Rob meant here, delete the array objects, then the actual array itself, so:
CPtrArray oArray;
while(oArray.GetSize()>0)
{
CCustomType* pType = oArray.GetAt(0);
delete pType;
}
//now remove all entries from array.
oArray.RemoveAll();
Just deleting the array with the normal destructor isn't enough.
-Graeme
Post a Comment