Re: Clean-up in OnDestroy
Hi Joseph,
If a thread is blocked on a semaphore what do you offer to do to terminate
that thread other than releasing the semaphore?
Suppose a thread proc like this :
while (bContinue)
{
if (WAIT_OBJECT_0 == WaitForSingleObject (hSemaphore, INFINITE))
{
// Land the spaceship...
}
else
{
// I always wonder what other programmers do here...
// My choice is landing the spaceship :)
}
}
If this thread blocked on the semaphore then we set bContinue to false and
relese the semaphore to terminate the thread gracefully :
bContinue = false;
ReleaseSemaphore (hSemaphore, ...);
Am I right now?
In respect of waiting threads to terminate :
I always think that terminating a process before it's all threads terminated
is a bad practice. So I initiate terminations for threads and wait them to
terminate. It sometimes gives useful information about idden bugs related to
thread termination. Off course, your warning about dead-lock should be noted
and threads should be leave alone with their terminations if requred to do
so.
And eventually, I should think about ActiveX related cases a bit more
considering your warning.
"Joseph M. Newcomer" <newcomer@flounder.com> wrote in message
news:0qvr24divnl5v03c6euatpv5764hmjjqlc@4ax.com...
OnClose would defiinitely be the wrong place to do this, and OnDestory
*might* the best
place to do this.
See below...
On Sat, 17 May 2008 00:14:25 +0300, "K?r?at" <xx@yy.com> wrote:
In clean up, I do :
- Call some ActiveX control's cleanup methods which cause some windows
messages generated and some ActiveX events occured
***
OnDestroy would be a good place to do this. WHo is supposed to receive
the messages
"generated". Note that you have to make sure that all these messages and
events are
processed synchronously, that is, before the call to the ActiveX control
returns, or you
can be left with dangling pointers (hence the *might* comment)
****
- Set some events (kernel objects) and release some semaphores to
terminate
worker threads and wait those threads to terminate by using
WaitForSingleObject ()
****
You would not use semaphores to terminate worker threads; this would be a
very bad choice
most of the time. You would use Events. If you are using semaphores, I
suspect you do
not know how to use them. It is generally a Bad Idea to wait for threads
to terminate.
They should terminate cleanly on their own. Note that if the theads do a
PostMessage,
they are safe, but the WFSO will deadlock the threads if they do
SendMessage. Overall,
not a good strategy.
****
- Close some handles,
****
Typical OnDestroy type of thing to do
****
- Deallocate some memory
****
Typical OnDestroy type of thing to do
****
That is all.
"AliR (VC++ MVP)" <AliR@online.nospam> wrote in message
news:XwhXj.2056$r82.572@nlpi069.nbdc.sbc.com...
What are you cleaning up?
If the cleanup does not require access to the controls then you should
put
the cleanup code in the destructor.
AliR.
"K?r?at" <xx@yy.com> wrote in message
news:OGiL391tIHA.4560@TK2MSFTNGP03.phx.gbl...
Hi,
In my dialog based MFC application I put all clean-up code into OnClose
(). Recently I noticed that calling EndDialog () closes the
application
but without executing OnClose (). So I move my clean-up code into
OnDestroy (). May this cause any validity problems?
Thanks in advance
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm