The Delphi Bug List

Entry No.
445
VCL - General - Classes - TStringList
Memory leakage with TStringList.AddObject (Sorted = TRUE; Duplicates = dupIgnore)
1.02 2.01 3.0 3.01 3.02 4.0 4.01 4.02 4.03 5.0 5.01 6.0 6.01 6.02 Kylix 1.0
ExistsExistsExistsExistsExistsExistsExistsExistsExistsExistsExistsFixedFixedFixedUnknown
Description
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.

Latest update of this entry: 2002-04-03

Post a comment on this bug


Index page
Delphi Bug List home page
The Delphi Bug Lists are presently maintained by Jordan Russell, who has taken over this task from Reinier Sterkenburg since August 2000.
All feedback is appreciated. See also the feedback section of the Delphi Bug List home page.