DestroyWindow 視窗建立和銷毀要在同一個線程裡

來源:互聯網
上載者:User
DestroyWindowRemarks

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. 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.