Memory leak in freeing ansistring arrays passed by value, that are modified in a procedure
Original Reporter info from Mantis: Bart @flyingsheep
-
Reporter name: Bart Broersma
Original Reporter info from Mantis: Bart @flyingsheep
- Reporter name: Bart Broersma
Description:
When you pass an array of ansistring (by value) to a procedure and then modify any of those strings inside the procedure a memory leak occurs (for each string modified).
(Possibly this only occurs if in the assigment that changes the string, the string itself is referenced on the right side of the assignment: see test program.)
This bug was originally reported and tested with fpc 2.4.2 but confirmed with 2.5.2 r17060 by Blaazen.
Tested on Linux 32/64 bit and win32.
The memory leak does not occur when ShortStrings are used.
Steps to reproduce:
Program project1;
{$mode objfpc}
{$h+}
procedure test(values: array of string);
begin
if paramcount = 0 then
values[0] := values[0] + '1'
else
values[0] := '1';
end;
begin
test(['test']);
end.
Compile with -gh
Output:
F:\LazarusProjecten\FpcProjecten>test
Heap dump by heaptrc unit
1 memory blocks allocated : 14/16
0 memory blocks freed : 0/0
1 unfreed memory blocks : 14
True heap size : 65536 (96 used in System startup)
True free heap : 65344
Should be : 65360
Call trace for block $004280DC size 14
$00401510 main, line 15 of test.dpr
$004089F1
F:\LazarusProjecten\FpcProjecten>test x
Heap dump by heaptrc unit
0 memory blocks allocated : 0/0
0 memory blocks freed : 0/0
0 unfreed memory blocks : 0
True heap size : 65536 (112 used in System startup)
True free heap : 65424
Additional information:
See also this thread on the forum: http://forum.lazarus.freepascal.org/index.php/topic,12292.0.html