Click here to Skip to main content
15,891,907 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi I have an MFC dll (not so expert on that). There I create a thread, and it creates some variable.
When closing the application that variables need to be destroyed of course...but I can't catch the right moment to do that so I get "Detected memory leaks!".
I have some code t odo the clean at the end of the thread but I can't find a way to exit from a loop inside it before it is too late.

It seems that when Dll::CWinApp::ExitInstance is called is too late and the thread was already brutally terminated.
The same for Dll::CWinApp::OnFinalRelease (just to try the way).
I tried to create a class to manage the variables and the thread (inside the dll) but also this time when it comes the destructor of the class it is too late and the thread is alredy destroyed.

I don't like to terminate the thread using the main program because I like to leave the thread hidden (I mean it is not exported).

So finally I can't find the right way to trap some event to set my flag to exit from my thread correcly.

Any helps
Thanks
Posted
Comments
Guyverthree 31-Mar-11 9:15am    
Is the thread message or event based ?

Or is it just going till the program quits ???
Russell' 31-Mar-11 9:23am    
HiIt is a simple worker thread (only file managing). It has to run always during the program run. Then I need to stop it correcly using a simple flag when the program stops.
I am trying to get some event from the CWinApp inside the dll (not the one in the exe, MFC application) to set that flag and wait that the thread exits itself, but it looks that everytime it is too late and the thread is already destroyed from somewhat before.
Guyverthree 31-Mar-11 9:39am    
Have you tried creating three events, one for there is some work and the other two for a quit.
Then wait on all three and if you have work do some, but if you need to quit clean up the thread and then set the exit event so you know the thread has completed and cleaned up.

This structure would also be the same sort of thing if you message based the thread instead of event based it.
Sergey Alexandrovich Kryukov 31-Mar-11 13:35pm    
It cannot be a solution because the application need to be closed at any moment, not only during wait state of the thread.
(Russel, I think you can ignore this idea.)
Also, OP has no problem with functionality -- the code is already working. OP wants to work cleanly in all phases of run-time.
--SA
Sergey Alexandrovich Kryukov 17-Apr-11 17:39pm    
There is a comprehensive solution. I did not have enough time to discuss it, but today I did. Please see -- very interesting and robust.
--SA

1 solution

There is one cunning and a very robust mechanism using the idea of see seeding of the exception.
It was invented by one former Microsoft engineer and was is implemented in .NET without much buzz about it. I used to fully implement it. It uses fully documented Windows API, but the non-portable part of it — you need to modify Instruction Pointer in thread's swapped out status data.

I tried to explain how it works in my comments on this page: How do I abort a specific thread that is already running?[^]. To locate it, please find the Answer by Olivier and my comments by the key words "exception seeding".

The key API to use is GetThreadContext (http://msdn.microsoft.com/en-us/library/ms679362(v=vs.85).aspx[^]) and SetThreadContext (http://msdn.microsoft.com/en-us/library/ms680632(v=vs.85).aspx[^]). What's important, you can call it from the thread other then the one to be aborted. You suspend the thread, get its content, modify Instruction Pointer to point to your function throwing the exception (define exception class, say "ThreadAbortException") and resume the thread. When you resume the thread, stack frame will be broken, but it does not matter, because you jump to exception throwing method (will never return). The thread body should run in the try-catch block, catch ThreadAbortException and perform all post mortal clean-up there. Most importantly, release all locks, etc.

To see what can you avoid by replacing TerminateThread with this mechanism, read help on this topic: http://msdn.microsoft.com/en-us/library/ms686717(v=vs.85).aspx[^]. You can avoid all problems (and many less difficult ones) by handling ThreadAbortException.

The implementation is not very difficult at all. It replaces unsafe TerminateThread with a very gentle way to abort it, leaving a thread with a possibility to process its post-mortal action accurately. You can even avoid termination of the thread by getting it all in a loop.

The implementation is fairly simple and the code is short, the mechanism is very robust.
Please ask your questions if you're interested, I'll be able to help you, but you better try to implement it yourself.

[EDIT]

See also my recent answer:
Problem with creating Process from dedicated thread[^].

—SA
 
Share this answer
 
v3
Comments
Espen Harlinn 18-Apr-11 18:05pm    
Interesting, my 5
Sergey Alexandrovich Kryukov 18-Apr-11 22:27pm    
Thank you, Espen.
This is very interesting, yet not too hard to implement. This is one of the little-known small things .NET grew out of. The only problem: I have trouble finding the original article. If and when I find it, I'll post a reference.
--SA
Olivier Levrey 19-Apr-11 9:32am    
Yes interesting. But I still think this technique is just a workaround.

If one can allocate resources or memory in a function or constructor, one can also free them on a destructor or whatever dedicated function. There is no need of exception seeding to release resources.

And by the way, OP solved the problem by puting the releasing code in a destructor, which is in my opinion a much better option.
Sergey Alexandrovich Kryukov 19-Apr-11 13:32pm    
There are few applications where this technique is unavoidable, as the only alternative is the brutal TerminateThread, which also can be use, but that would be a ***real*** work-around, a risky kind.

Calling that destructor is not the only case.

Here is one case: imaging your application is a host for user-written plug-in; the user can write initialization, clean-up and run method. The plug-in operation should be aborted by any time by the host. Needless to say, the user is not willing to cooperate in checking "abort requested" or something. How to guarantee the clean-up method is called regardless of the time of the abort? More importantly, how to organize the abort itself at any moment of time?

You see, this is a problem. I perfectly understand why this method was invented.

Other examples include hosting complex calculation type (numeric simulation) and control of mission critical hardware process which may need emergency stop...

--SA
Sergey Alexandrovich Kryukov 12-Jun-12 0:03am    
Please see my update to the answer, referencing my other answer, after [EDIT].
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900