Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use TerminateThread without cause memory leak ?

Status
Not open for further replies.

Tommi1900

Programmer
Nov 29, 2007
3
IT
MSDN reports that terminatethread cause a memory leak.
Is it possible track resource allocated by a thread and free them after I forced it to terminate ?

Thanks
 
are you using the VCL TThread class?

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
you can always use the onterminate event to free your objects...

Is this a problem you are having? please show us some code then.

I tend to do everything in the OnExecute method.

like this (psuedo code)


Code:
procedure TLinkThread.Execute;
begin
 Event := CreateEvent(nil, True, False, nil);
 MyObject := TMyObject.Create;
 try
  while not Terminated do
   begin
    Sleep(10); // prevent 100% cpu
    DoSomeWork;
   end;
 finally
  FreeAndNil(MyObject);
 end;
 SetEvent(Event); // inform thread creator we are done here
 Sleep(100);
 CloseHandle(Event);
end;

never had memory leaks like this when calling Thread.Terminate


-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Yes but sometimes I need to kill my thread bacause I'm using Synapse libraries to make an http connection. I set Timeout, but sometimes, I don't know why, my thread hangs during download. To prevent this I make so that the creator's thread wait 10 secs and try to force to kill it.
With DoTerminate it isn't possible, so I need some windows api to force to kill.
The hangs are very rare, but in many hours if I cause every time a memory leak may be I waste a lot of memory.
 
search the error then. killing a thread should not be common practice, and yes it causes memory leaks (the TLS variables delphi creates are not freed)

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top