Re: Program Crashes On Exit
"billyard" <dmetcalf@columbus.rr.com> wrote in message
news:4758854a$0$2370$4c368faf@roadrunner.com...
I hope I can explain this okay. I have a multi-threaded application
developed using Microsoft Visual C++ 2005 (using MFC). During the execution
of the main thread I have a loop. This loop is used for retrieving email -
all the emails I have on my POP3 server. Everything works fine - unless I
exit the program while it is still retrieving the mail. I retrieve the mail
in a separate thread and I believe the thread itself exits in an okay
manner. The main thread has pointers that point to 0xFEEE. This has to be a
common problem (at least amongst beginners like me). Any help would be
greatly appreciated. Man, I hope this is enough detail - but I will gladly
provide more if it's needed.
Thanks again.
There is only one way to be sure the secondary thread exits cleanly when
your program is shutting down. You have to signal the secondary thread (any
number of ways) to exit, then you must NOT let the main thread exit, or
destroy anything shared with the thread, until you get a signal back from
the secondary thread that it has exited. In other words, you have to force
things to quit in a safe order.
One way is to call WaitForSingleObject on the thread handle. You do this in
CMainFrame::OnClose, so the main thread postpones its shutdown until the
secondary thread has exited.
--
Scott McPhillips [VC++ MVP]