Recent

Author Topic: TThread.Queue Causing Memory Leak  (Read 3759 times)

kodok.buncit

  • New Member
  • *
  • Posts: 35
TThread.Queue Causing Memory Leak
« on: May 11, 2014, 02:39:36 am »
Hello, i play a bit with TThread.Queue
the following code cause memory leak on my machine(see signature)

Code: [Select]
Type
  { TTest }

  TTest = class
  procedure sayHello();
  end;

{ TTest }

procedure TTest.sayHello;
begin
  WriteLn('hello world');
end;

var
  test: TTest;
begin
  test := TTest.Create;
  try
    TThread.Queue(TThread.CurrentThread,@test.sayHello);
  finally
    test.Free;
    //TThread.CurrentThread.Free;
  end;


however if i uncomment the last code ( TThread.CurrentThread.Free; ) all memory cleanup successfully.

is this intended behaviour? or may be a bug?

hanks in advance



« Last Edit: May 11, 2014, 02:41:56 am by kodok.buncit »
kodok.buncit on github https://github.com/kodok-buncit/

Primary OS : Linux Ubuntu 12.04
fpc & laz      : always Trunk version

Blaazen

  • Hero Member
  • *****
  • Posts: 2782
  • POKE 54296,15
    • Eye-Candy Controls
Re: TThread.Queue Causing Memory Leak
« Reply #1 on: May 11, 2014, 11:58:43 am »
Just use Alt+Up and Ctrl+Shift+Down to see what the code do. TThread,Queue is class method which uses property GetCurrentThread and it's getter is:
Code: [Select]
class function TThread.GetCurrentThread: TThread;
begin
  { if this is the first time GetCurrentThread is called for an external thread
    we need to create a corresponding TExternalThread instance }
  Result := CurrentThreadVar;
  if not Assigned(Result) then begin
    Result := TExternalThread.Create;
    CurrentThreadVar := Result;
  end;
end;
So yes, you need to free it.
Lazarus 2.1.0 r61214:62238 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

vfclists

  • Hero Member
  • *****
  • Posts: 825
    • HowTos Considered Harmful?
Re: TThread.Queue Causing Memory Leak
« Reply #2 on: June 01, 2014, 11:24:27 pm »
Does this mean that FreePascal needs its own garbage collected memory manager? This is one of the aspects I have discovered, in that even if you free what you created yourself, that creation may have also created something else requiring freeing that you don't know about.

Other than waiting for memory management dialogs at the end, or using heaptrc is there a way of knowing about these in advance?

Just use Alt+Up and Ctrl+Shift+Down to see what the code do. TThread,Queue is class method which uses property GetCurrentThread and it's getter is:
Code: [Select]
class function TThread.GetCurrentThread: TThread;
begin
  { if this is the first time GetCurrentThread is called for an external thread
    we need to create a corresponding TExternalThread instance }
  Result := CurrentThreadVar;
  if not Assigned(Result) then begin
    Result := TExternalThread.Create;
    CurrentThreadVar := Result;
  end;
end;
So yes, you need to free it.
Windows - Lazarus 1.0.8/FPC 2.6.0
Linux - Lazarus 1.4.4 /FPC 2.6.4

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7506
Re: TThread.Queue Causing Memory Leak
« Reply #3 on: June 02, 2014, 12:56:49 pm »
Does this mean that FreePascal needs its own garbage collected memory manager?

No, FPC is not garbage collected. Users must deallocate the memory specific, except for certain automated types, and tthread is not one of those.