SendMessage和 PostMessage; 使用PostMessage(WM_QUIT)退出程序时导致的内存泄漏问题

引言:我们要使用代码关闭程序的话,应该向窗口发送WM_CLOSE或者直接调DestroyWindow(HWND)函数   (默认情况下WM_CLOSE的消息响应就是调用DestroyWindow(HWND)   函数,所以我们直接调用也达到一样的效果).这样可以令操作系统回收窗口占用着的内存资源后再退出程序.

千万不要直接用PostMessage(WM_QUIT);令程序退出,这样程序是可以退出了,但是窗口占用的内存资源就没办法回收,久而久之程序就运行出错了.

-------------------------------------------------------------------------------------------------------

使用PostMessage(WM_QUIT)退出程序时导致的内存泄漏

 

先看一下MSDN的介绍:

The SendMessage function sends the specified message to a window or windows. It calls the window procedure for the specified window and does not return until the window procedure has processed the message.

The PostMessage function places (posts) a message in the message queue associated with the thread that created the specified window and returns without waiting for the thread to process the message.

   


 从MSDN的解释上看,感觉SendMessage比PostMessage更加安全,因为,SendMessage要等到窗体消息处理完才返回,而PostMessage发送消息后就立即返回,这更令人茫然。既然从这上面看不出SendMessage无法实现的原因,那么就从消息WM_QUIT上查。

The WM_QUIT message indicates a request to terminate an application and is generated when the application calls the PostQuitMessage function. It causes the GetMessage function to return zero.

    当应用程序调用PostQuitMessage函数的时候, WM_QUIT消息终止一个程序的,它促使GetMessage函数返回0。

    可这里根本就没提SendMessage无法完成终止程序的原因。但在Remark中,却有这样一句:

Do not post the WM_QUIT message using the PostMessage function; use PostQuitMessage。至于为什么,就得看PostQuitMessage做什么事了。现在不讨论为什么不让使用PostMessage发送WM_QUIT消息,但就为什么SendMessage无法完成PostMessage能完成的功能来做一下研究。

注意到WM_QUIT消息让消息循环终止归根结底是让GetMessage返回为0,而GetMessage函数是从消息对列里取一条消息,然后再返回,只能当消息为WM_QUIT时才返回0结束消息循环。再仔细看一下SendMessage的注释发现,SendMessage直接发送到窗口,并调用窗口处理程序,完成消息响应,即SendMessage 根本就没有将消息发到消息对列中,因此GetMessage无法从消息对列中收到WM_QUIT的消息。而PostMessage却直接发送消息到消息对列中,然后立即返回,这一点点的区别就解决了我们上面的问题。

    了解了这一点,就不难理解上面注释中说的为什么不让直接使用PostMessage来发送WM_QUIT消息来终止程序了。

    另外需要注意的是,发送消息让对话框关闭,应该发送WM_CLOSE或者直接调用)消息,这样可以让对话框完成它自身的资源释放回收。

猜你喜欢

转载自blog.csdn.net/u013228754/article/details/82457203