**:
分類: vc
2009-08-31 18:48
4220人閱讀收藏
舉報dialog
windows
null
delete任務
對於乙個非模態子對話方塊的記憶體釋放,一般的處理方法是在onclose()函式中新增destroywindow(),然後在postncdestroy()中delete this。
當我們為了使得生成的非模態子對話方塊最小化的時候在工作列上顯示出來,建立的時候就要以桌面為父視窗,如下:
pdlg->create( idd_dialog_child,getdesktopwindow());
這種情況下,如果你先關閉非模態子對話方塊,再關閉主對話方塊是沒有問題的。但當你關閉主對話方塊的時候,如果這時還有前面說的非模態子對話方塊存在,那麼就會造成記憶體洩露。原因是此時pdlg並沒有接收到任何關閉的訊息。
我們的解決方法是在父視窗中儲存乙個非模態子對話方塊的指標pdlgcopy,這個指標在pdlg存在的情況下要賦值,在pdlg銷毀的情況下要清零。然後在主對話方塊關閉的時候檢查這個指標,如果不為空,則釋放。
現在又有新的問題,如果你在pdlg裡面的操作分配了記憶體,但是你是在pdlg的postncdestroy()中釋放的話,那麼還是會有記憶體洩露,因為在父視窗中刪除那個非模態對話方塊指標的時候,只會進pdlg的析構函式,而不進postncdestroy()。所以要麼你在析構函式中釋放記憶體,要麼你在父視窗中呼叫pdlgcopy->destroywindow()。前者要注意此時this指標和物件控制代碼都已為null,而且你的子控制項中的記憶體釋放是否依賴於wm_destroy,後者方法要注意你本來destroywindow()中就有把pdlgcopy置為null的操作,如果對話方塊指標多了要用模板儲存,還要注意迭代器失效的問題。
為什麼pdlg接收不到任何關閉的訊息?
關於視窗的parent和owner的區別:
下面是msdn中的解釋:
summary
in the windows environment, the following two relationships can exist between windows:
owner-owned relationship. the owner-owned relationship determines which other windows are automatically destroyed when a window is destroyed. when window a is destroyed, windows automatically destroys all the windows that are owned by a.
parent-child relationship. the parent-child relationship determines where a window can be drawn on the screen. a child window (that is, a window that has a parent) is confined to its parent window's client area.
this article discusses these relationships and some windows functions that provide owner and parent information for a specified window.
下面這個例子證明「the owner-owned relationship determines which other windows are automatically destroyed when a window is destroyed. 」彷彿是錯誤的。
[cpp]view plain
copy
// 能收到關閉訊息
void
cstaticmultidlg::onbuttoncreate()
cwnd *powner = pdlg->getowner(); // powner 0x00000000
} // 能收到關閉訊息
void
cstaticmultidlg::onbuttoncreate()
cwnd *powner = pdlg->getowner(); // powner 0x00000000
} // 收不到關閉訊息
void
cstaticmultidlg::onbuttoncreate()
cwnd *pparent = pdlg->getparent(); // pparent 0x00000000
cwnd *powner = pdlg->getowner(); // powner 0x00000000
} // 收不到關閉訊息
void
cstaticmultidlg::onbuttoncreate()
cwnd *pparent = pdlg->getparent(); // pparent 0x00000000
cwnd *powner = pdlg->getowner(); // powner 0x0012fdf4 == this
} // 這種情況下程式會假死
void
cstaticmultidlg::onbuttoncreate()
cwnd *pparent = pdlg->getparent(); // pparent 0x00000000
cwnd *powner = pdlg->getowner(); // powner 0x0012fdf4 == this
}
MFC 模態對話方塊與非模態對話方塊釋放資源的小總結
對於模態對話方塊,其實它是在堆上建立的物件,當函式結束後會自動釋放其資源。但對於非模態對話方塊資源的釋放就沒這麼直接,原因 1 它是在堆上建立的。2 在當前的使用函式內是不能直接用delete刪除的。後經過查資料才了解怎麼樣處理,對非模式對話方塊做了些總結如下 a.如何建立非模式對話方塊 建立函式 ...
模態對話方塊與非模態對話方塊
1.對話方塊分類 按工作方式不同,可將對話方塊分成兩類 模態對話方塊 modal 在關閉模態對話方塊之前,程式不能進行其他工作 如一般的 開啟檔案 對話方塊 非模態對話方塊 modeless 非模態對話方塊開啟後,程式仍然能夠進行其他工作 如一般的 查詢與替換 對話方塊 2.對話方塊建立 模態對話方...
模態對話方塊和非模態對話方塊
模態對話方塊和非模態對話方塊的區別 在這裡我就說的比較通俗易懂了,就是當你開啟乙個模態對話方塊時,你的焦點不能轉移到程式的其他視窗上,也就是你只能先響應模態對話方塊,才能進行成下面的操作。而非模態對話方塊則相反,你可以不用管它,照常能夠將其他視窗啟用。建立模態對話方塊,主要是應用對話方塊的domod...