您的位置:首页 > 其它

DestroyWindow 窗口创建和销毁要在同一个线程里

2012-07-26 17:52 232 查看

DestroyWindow

Remarks

A thread cannot use DestroyWindow to destroy a window created by a different thread.

If the window being destroyed is a child window that does not have the WS_EX_NOPARENTNOTIFY style, aWM_PARENTNOTIFY message is sent to the parent.

有些窗口是写在 dll 里的,创建和销毁都有对外接口。
上面的是 MSDN 的一些描述。说的就是创建和销毁不在同一个线程里会出现问题。

问题:
前些时候自己调整了VC编译器的配置,全部编译都不优化,生成相应的 pdb 文件,这种情况如果出现问题的话,就很容易暴露。
别人的代码,创建窗口是在 dll 外的线程里进行,但是在响应菜单的时候,却在原来窗口里进行 DestroyWindow。
在 windows xp 的机器上没有问题,但是在 win7 下就没有任何提示退出了。还好问题能重现,用 windbg 绑定进程进行跟踪,进程崩溃了
就看堆栈,每次定位的堆栈都不一样,这种情况很大的可能性是内存错乱,或者消息发给已经销毁的窗口处理,或者代码调用已经销毁的窗口的某些接口导致的
不确定性报错。

结果:
用 windbg 无法定位问题,只好走一下原理代码的逻辑了,后来发现 DestroyWindow 的代码调用多次而且逻辑有错误,查一下 MSDN 就明白问题在哪里了。
将 DestroyWindow 改为 PostMessage(WM_CLOSE) 就解决问题了。

Remarks

An application can prompt the user for confirmation, prior to destroying a window, by processing the
WM_CLOSE message and calling the DestroyWindow function only if the user confirms the choice.

By default, the DefWindowProc function calls the
DestroyWindow function to destroy the window.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: