正确关闭dll中的线程 [英] Close correcly the thread inside a dll

查看:196
本文介绍了正确关闭dll中的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我有一个MFC dll(对此不是很熟练).我在那里创建了一个线程,它创建了一些变量.
关闭应用程序时,变量当然需要被破坏...但是我无法抓住正确的时机这样做,所以我得到检测到内存泄漏!".
我在线程末尾有一些干净的代码,但是在为时已晚之前,我找不到从其中的循环退出的方法.

似乎在调用Dll :: CWinApp :: ExitInstance时为时已晚,并且该线程已被残酷地终止.
Dll :: CWinApp :: OnFinalRelease也是一样(只是尝试一下).
我试图创建一个类来管理变量和线程(在dll内部),但是这一次,当它涉及到该类的析构函数时,为时已晚并且线程已被破坏.

我不喜欢使用主程序终止线程,因为我想使线程保持隐藏状态(我的意思是不导出).

所以最后我找不到正确的方法来捕获某些事件以将我的标志设置为正确地从线程退出.

任何帮助
谢谢

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

推荐答案

有一种狡猾的机制,它使用了查看异常种子的想法.
它是由一位前Microsoft工程师发明的,并在.NET中实现,而对此却知之甚少.我曾经完全实现它.它使用了完整记录的Windows API,但是它的不可移植部分-您需要在线程换出的状态数据中修改指令指针.

我试图在此页面的评论中解释它的工作原理: ^ ].要找到它,请通过关键词"exception seeding"找到"Olivier的答案"和我的评论.

要使用的关键API是GetThreadContext( http://msdn.microsoft .com/en-us/library/ms679362(v = vs.85).aspx [http://msdn.microsoft.com/en-us/library/ms680632(v=vs.85).aspx [ThreadAbortException"),然后继续执行线程.当您恢复线程时,堆栈框架将被破坏,但这并不重要,因为您将跳转到异常抛出方法(永远不会返回).线程主体应在try-catch块中运行,捕获ThreadAbortException并在那里进行所有凡人清理.最重要的是,释放所有锁等.

若要查看通过使用此机制替换TerminateThread可以避免什么,请阅读有关此主题的帮助:
从专用线程创建Process的问题 [ ^ ].

—SA
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.



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

—SA


这篇关于正确关闭dll中的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆