Gracefull thread killing when App ends

Posted on 1998-11-19
Last Modified: 2013-11-20
My app will sometimes create a CWinThread-derived thread to go do some work.  It is technically a UI thread, since it has a window, used to monitor what is going on, but it does not get any user input.

I have the thread set up with the m_bAutoDelete to TRUE, so that once the tread is finished it deletes itself.  This works quite well until I try quitting the program while the thread is still running.  When this happens the thread gets killed, and memory leaks abound.

So I thought, when the app is quitting, I could somehow inform the thread that it is time to die.  However, because the thread can destroy itself, I don't know if I have a thread still running or not.  The thread destroys itself, but my pointer still has a value, alas one that is useless.

How can I figure out if the thread is still running when I quit, and if so tell it to quit before the app finishes?
Question by:carlosn
  • 3
  • 2

Author Comment

ID: 1325008
Edited text of question

Expert Comment

ID: 1325009
When you quit the application all threads of this will be automaticaly killed. Now your problem with memory leaks can be easily solved by only puting sleep(NNNN) in your WM_ONCLOSE member function of CFrmWnd Class. NNNN denotes milliseconds sleep(2000). That depends on your user interface thread. You try it out with changing the values of NNN and see in debug mode whether  your user interface thread is giving any memory leak or not. Please keep updating the interaction.

Author Comment

ID: 1325010
I thought about doing this, but I would like some option that is a bit more graceful.  I'd rather not have an absolute wait, especially since most of the time the thread is already dead anyway.  The issue is only when the user happens to kill the app during the brief time the thread is running.
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

LVL 11

Accepted Solution

mikeblas earned 50 total points
ID: 1325011
There's no silver bullet here.

You have to code your threads to watch for an event or look for a flag. When they see that signal, they should exit.

When you want to exit, set that flag and then wait for the threads to stop executing. You can wait for a thread by calling WaitForSingleObject() (or any of its friends) on the handle of the thread.

Don't ever, under any circumstances, call the TerminateThread() API.

B ekiM


Author Comment

ID: 1325012
I see what you are getting at here.  I hadn't thought of that approach.  My only concern, if the thread deletes itself after completion, there isn't a handle to wait on any more.  Say I had a thread pointer 'pThread'

The thread executed, and then deleted itself because of the m_bAutoDelete = TRUE.  What happens if I try a WaitOnSingleEvent(pThread->m_hThread);  It seems that there is a slim chance that whatever happens to lie in the memory once occupied by the variable could point to a waitable object, though I guess that is fairly slim.

LVL 11

Expert Comment

ID: 1325013
In actuality, there is.  The thread handle is valid until someone calls CloseHandle() on it.

The CWinThread object, on the other hand, can delete itself as you describe. And, for that reason, m_bAutoDelete is of dubious value in the class.

B ekiM

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now