TStringList.SetCapacity bugs
Original Reporter info from Mantis: Derek
-
Reporter name:
Original Reporter info from Mantis: Derek
- Reporter name:
Description:
Resolution of bug 0024943 has introduced another issue. If the capacity is set to a value greater than FCount, but less than the current FCapacity, then FCount is incorrectly set to NewCapacity. The extra items contain random data which can cause an access violation when cleared.
A separate issue relates to TStringList.Capacity = 0. This bug preceeds 0024943 and will result in a memory leak if the current value of FCount is not 0. SetCapacity simply calls FreeMem without first freeing any string values. It should first call InternalClear(0,False) to release the strings/objects. ClearOnly must be set to false to prevent a loop. This does not apply to either Destroy or Clear, only when manually setting Capacity = 0.
Steps to reproduce:
// Issue 1
t := TStringList.Create;
t.add('test');
writeln(inttostr(t.count)+','+inttostr(t.capacity));
// output: 1,4
t.capacity := 2;
writeln(inttostr(t.count)+','+inttostr(t.capacity));
// output: 2,2
// t[1] may not be '', then setting t[1]:='' causes access violation
t.free
// Issue 2
t := TStringList.Create;
t.add('test');
t.Capacity := 0;
// t[0] is not released
Additional information:
I have uploaded a patch for stringl.inc which fixes both issues
Mantis conversion info:
- Mantis ID: 25983
- Version: 2.6.3
- Fixed in version: 3.0.0
- Fixed in revision: 26053 (#fd9b32f8)
- Target version: 3.0.0