Thread object leak

Giganews Newsgroups
Subject:Thread object leak
Posted by: mikk (mikk@nospamplease.go2.pl)
Date:Tue, 27 Mar 2007

Hi everyone,

I have got a problem with my TThread descendant. The MemCheck reports
memory leak on thread object instance which I create. Since I'm setting
FreeOnTerminate := True to avoid WaitFor synchro hell, I don't call the
destructor, intentionally. But it seems there is a problem with that.

I use the thread just like this:

myThread := TMyThread.Create(True); // <<-- MemCheck log shows leak here
try
  myThread.FreeOnTerminate := True;
  myThread.Resume();
  // Long operations - Here I'm posting messages to the
  // message queue of the thread to process data in background
finally
  myThread.Terminate();
  // Usually there should stand myThread.Free(),
  // but since its FreeOnTerminate = True, it should
  // be freed by itself when Execute() ends.
  // When I call Free() or WaitFor() and Free(),
  // then I have a deadlock.
end;

I use Delphi 7 (patched). What can I do to avoid this leak ???

Thanks in advance,
mikk

Replies