|
Reported by Michael Phillips; checked by Reinier Sterkenburg
The problem occurs when we define a TStringList with 'Sorted = TRUE', and
'Duplicates = dupIgnore'. When performing an AddObject(string, object) of
a string that already exists in the TStringList, the TStringList object
pointer is changed to point to the passed object which discards the
previous object. There now is no pointer pointing to the discarded object so
we have a memory leak.
AddObject('string1', object1);
AddObject('string2', object2);
AddObject('string3', object3);
AddObject('string2', object4);
In the brief example above the resulting TStringList is as below:
string1 object1
string2 object4
string3 object3
and the pointer to object2 is lost.
I'd expect it to be
string1 object1
string2 object2
string3 object3
with a fail flag returned so that I can dispose of the object I created (in
this instance object4).
eg.
object4 := create;
if (AddObject('string2', object4) = -1) then
object4.Free;
In the online help for Delphi under TStringList.Duplicates it says that
dupIgnore should 'ignore attempts to add duplicate string to the sorted
list'. This is not the case when doing AddObject.
In Delphi 6, the object is not added so there is no direct memory leak anymore.
However, the result value of AddObject is the index of the 'already present'
item in the list, so the 'if ... = -1' check will not work. |