Click to See Complete Forum and Search --> : How to free Thread resource (CString) when it is terminated
AshwinRao
July 11th, 2005, 07:28 AM
Hello everyone,
How can I free a CString initialized intialized inside a worker thread busy doing work in a while loop, but suddendly teminatd by the user.
Thanks in Advance,
Regards,
Ashwin
kuphryn
July 11th, 2005, 01:16 PM
Pointers - one solution is to pass in a pointer of the object declared in the main thread.
Kuphryn
Arjay
July 11th, 2005, 01:30 PM
Hello everyone,
How can I free a CString initialized intialized inside a worker thread busy doing work in a while loop, but suddendly teminatd by the user.
Thanks in Advance,
Regards,
AshwinI would have to check for sure, but don't destructors get called for objects inside a thread proc (even when the thread has been terminated)? If this is true (you'll have to check for sure), then the CString will cleanup automatically providing it's a stack based object.
What you may want to look at is providing a mechanism that allows a user to prematurely exit the thread. The way you've worded your question leads me to believe that when the user ends the thread, the code internally uses TerminateThread. Is this correct?
If so consider revising your thread proc, to allow for an graceful exit path. One way to do this is to pass a shutdown event handle to the thread, and during the processing loop, you frequently check if the event has been set. If it's been set, you clean up and exit the thread. With this method, when the user closes the thread, the shutdown event is set (rather than terminatethread getting called, if that's what was happening).
Arjay
Siddhartha
July 11th, 2005, 06:20 PM
...don't destructors get called for objects inside a thread proc (even when the thread has been terminated)?Destructors dont get called in this case.
When a thread is Terminated (using TerminateThread) the CString will not be destructed.
This applies to any (non-POD) object <-- The destructor will not be called on termination by brute-force.
In other words, even if the memory (of the CString object itself) is cleared as the stack gets popped (perhaps trashed?) the objects are not destroyed in the correct sense i.e. they are not destructed.
Therefore, any memory allocated to the underlying pointer (LPTSTR) on the heap will leak.
The correct way to end a thread is to return from it.
Andreas Masur
July 12th, 2005, 02:00 AM
How to end a thread? (http://www.codeguru.com/forum/showthread.php?t=305166)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.