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

来源:互联网 发布:岭南大学 知乎 编辑:程序博客网 时间:2024/05/21 09:37

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 theWM_CLOSE message and calling theDestroyWindow function only if the user confirms the choice.

By default, the DefWindowProc function calls theDestroyWindow function to destroy the window. 


即时通讯经验交流群:1535327732


原创粉丝点击