Memory not freed for AnsiString typed const declared in local scope
Original Reporter info from Mantis: zoran
-
Reporter name: Zoran Vučenović
Original Reporter info from Mantis: zoran
- Reporter name: Zoran Vučenović
Description:
If AnsiString typed constant is declared locally in a function, then if we change it in runtime, memory is not freed.
Steps to reproduce:
Compile this program with heaptrc (-gh), and run it:
program project1;
{$mode objfpc}{$H+}
function GetAB: String;
const
S: String = 'A';
begin
if S = 'A' then
S := S + 'B';
Result := S;
end;
begin
writeln(GetAB);
end.
I get:
1 memory blocks allocated : 27/32
0 memory blocks freed : 0/0
1 unfreed memory blocks : 27
True heap size : 32768
True free heap : 32576
Should be : 32608
Call trace for block $00007FFFF7FF20C0 size 27
$0000000000400280 main, line 15 of project1.lpr
Additional information:
-
if we change the line "S := S + 'B';" to "S := 'AB';" the bug disappears!
-
If the const declaration is moved to outer scope, the bug disappears (the memory is freed):
program project1;
{$mode objfpc}{$H+}
const
S: String = 'A';
function GetAB: String;
begin
if S = 'A' then
S := S + 'B';
Result := S;
end;
begin
writeln(GetAB);
end.
Now I get:
1 memory blocks allocated : 27/32
1 memory blocks freed : 27/32
0 unfreed memory blocks : 0
True heap size : 32768
True free heap : 32768
Mantis conversion info:
- Mantis ID: 33804
- OS: Linux Mint
- OS Build: 18.3
- Build: 39141
- Platform: x86_64
- Version: 3.1.1