Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2023-03

Memory leak: bug ot as designed?

4 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Bart

Hi,

The following program gives a memory leak:
===
program test;
{$mode objfpc}
{$h+}
uses
  Classes;
var
  SL: TStringList;
procedure Foo;
const
  S: String = '';
begin
  S := SL.Text;  //  << this is line 13
end;
begin
  SL := TStringList.Create;
  SL.Add('1');
  Foo;
  SL.Free;
end.
===
C:\Users\Bart\LazarusProjecten\ConsoleProjecten>fpc test.pas -gl -gh
Free Pascal Compiler version 3.3.1 [2022/10/11] for i386
Copyright (c) 1993-2022 by Florian Klaempfl and others
Target OS: Win32 for i386
Compiling test.pas
test.pas(11,3) Note: Local variable "S" is assigned but never used
Linking test.exe
21 lines compiled, 0.3 sec, 170976 bytes code, 7332 bytes data
1 note(s) issued

C:\Users\Bart\LazarusProjecten\ConsoleProjecten>test Heap dump by heaptrc unit of
"C:\Users\Bart\LazarusProjecten\ConsoleProjecten\test.exe" 117 memory blocks allocated : 2859/3152
116 memory blocks freed : 2843/3136
1 unfreed memory blocks : 16
True heap size : 294912 (272 used in System startup) True free heap : 294512
Should be : 294528
Call trace for block $016E93B0 size 16
$00401787 FOO, line 13 of test.pas
$004017E9 main, line 18 of test.pas

Is that a bug, or a user error?

Bart

Reply by email

Bart
to Ondrej Pokorny
FYI: Delphi doesn't create a memory leak.
The memory leak seems to be related to string concatenation in the
getter for the Text property.
A getter like this is enough to get the leak:
function TBar.GetTextStr: String;
begin
  Result := 'X';
  Result := Result + 'Y';
end;

If the initial assigment to Result is an empty string, then no leak occurs.

I'll report it in the bugtracker.

Bart

Reply by email