David wrote:
I use an exception handler MadException that also reports on memory
leaks (BTW, a great tool!). It is indicating I have a memory leak in
the following program (example modified from the Delphi Basics site to
illustrate the error). The memory leak reported occurs during the
TCustomer.Create.
TList is just a list of memory pointers. You are responsible for managing
the lifetimes of any objects you add to the list. Since you are not freeing
the object in your code, they are leaked. You need to free them manually,
eg:
myList := TList.Create;
...
myList.Add(TCustomer.Create('Bill Gates', 123)); //Memory leak caused here;
myList.Add(TCustomer.Create('Steven Jobs', 456)); //Memory leak caused here
...
// add this!
for I := 0 to myList.Count-1 do begin
TCustomer(myList[I]).Free;
end;
...
FreeAndNil(myList);
If you want the list to take ownership of the objects and free them automatically
for you, use a TObjectList instead, and set its OwnsObjects property to True
(which it is by default).
--
Remy Lebeau (TeamB)
Connect with Us