Forum > General

TThread.Queue Causing Memory Leak

(1/1)

kodok.buncit:
Hello, i play a bit with TThread.Queue
the following code cause memory leak on my machine(see signature)


--- Code: ---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;

--- End code ---


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



Blaazen:
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: ---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;
--- End code ---
So yes, you need to free it.

vfclists:
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?


--- Quote from: Blaazen 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: ---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;
--- End code ---
So yes, you need to free it.

--- End quote ---

marcov:

--- Quote from: vfclists on June 01, 2014, 11:24:27 pm ---Does this mean that FreePascal needs its own garbage collected memory manager?

--- End quote ---

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

Navigation

[0] Message Index

Go to full version