• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            牽著老婆滿街逛

            嚴(yán)以律己,寬以待人. 三思而后行.
            GMail/GTalk: yanglinbo#google.com;
            MSN/Email: tx7do#yahoo.com.cn;
            QQ: 3 0 3 3 9 6 9 2 0 .

            Destroying Window Objects(銷毀窗口對象)

            轉(zhuǎn)載自:http://hi.baidu.com/%BF%AA%D0%C4_%D0%D6%B5%DC/blog/item/915910816dd4f6dfbd3e1ea9.html

            TN017: Destroying Window Objects

            銷毀窗口對象

            此章節(jié)講述了CWnd::PostNcDestroy成員函數(shù)的使用。如果你想對派生自CWnd的對象做自定義的處理,你可以使用這個函數(shù)。

            此章節(jié)也闡述了“要銷毀一個C++Windows對象,使用DestroyWindow而不是delete”這個最重要規(guī)則的原因。

            這是非常重要的。如果你遵照以下的指導(dǎo)方法,你就會很少碰到清除方面的問題(例如忘記刪除/釋放C++內(nèi)存,忘記釋放系統(tǒng)資源比如說HWNDs,或者釋放對象太多次)。

            問題

            Windows對象(CWnd派生類的對象)既代表一個C++對象(在應(yīng)用程序的堆中分配)也代表了一個HWND(由窗口管理器在系統(tǒng)資源里分配)。由于存在多種途徑來銷毀一個窗口對象,我們必須提供一組規(guī)則以防止系統(tǒng)資源或者應(yīng)用程序的內(nèi)存泄露問題,同時也防止對象和Windows句柄被多次銷毀。

            這不僅僅是一個內(nèi)存管理的問題。一個Windows窗口的存在會對用戶界面產(chǎn)生影響:窗口繪制在屏幕上;一旦它被銷毀,同樣對系統(tǒng)資源也會產(chǎn)生影響。在你的應(yīng)用程序地址空間里泄漏C++內(nèi)存并不會像泄漏系統(tǒng)資源那樣糟糕。

            銷毀窗口

            有兩種方法被允許來銷毀一個Windows對象:

            l         調(diào)用CWnd::DestroyWindowWindows API ::DestroyWindow.

            l         利用delete操作符來進行明確的刪除工作。

            第一種方法是迄今為止最常用的。即使DestroyWindow沒有在你的代碼里被直接調(diào)用,此方法也照常適用。這種情況就是,當(dāng)用戶直接關(guān)閉一個框架窗口時(缺省的WM_CLOSE行為主是調(diào)用DestroyWindow),當(dāng)一個父窗口(框架窗口)被銷毀時,Windows會調(diào)用DestroyWindow來銷毀它的所有的子窗口

            Auto Cleanup with CWnd::PostNcDestroy

            When destroying a Windows window, the last Windows message sent to the window is WM_NCDESTROY. The default CWnd handler for that message (CWnd::OnNcDestroy) will detach the HWND from the C++ object and call the virtual function PostNcDestroy. Some classes override this function to delete the C++ object.

            利用CWnd::PostNcDestroy進行自動清除

            當(dāng)銷毀一個Windows窗口時,最后發(fā)送給此窗口的Windows消息是WM_NCDESTROYCWnd對此消息的缺省處理(CWnd::OnNcDestroy)會將C++對象與HWND分離,并調(diào)用虛函數(shù)PostNcDestroy一些類重載這個函數(shù)來刪除C++對象。

            The default implementation of CWnd::PostNcDestroy does nothing which is appropriate for window objects allocated on the stack frame or embedded in other objects. This is not appropriate for window objects that are designed to be allocated by themselves on the heap (not embedded in other C++ object).

            CWnd::PostNcDestroy的缺省操作是什么也不做,這適合于那些分配在堆棧或者嵌在其他對象里面的窗口對象。這不適合于那些設(shè)計來分配在堆上的窗口對象(不嵌在其他C++對象中)。

            Those classes that are designed to be allocated by themselves on the heap override the PostNcDestroymember function to perform a "delete this". This statement will free any C++ memory associated with the C++ object. Even though the default CWnd destructor calls DestroyWindow if m_hWnd is non-NULL, this does not lead to infinite recursion since the handle will be detached and NULL during the cleanup phase.

            那些設(shè)計來分配在堆上的類可以重載成員函數(shù)PostNcDestroy以執(zhí)行“delete this”操作。它將會釋放任何與此C++對象相關(guān)的C++內(nèi)存。盡管缺省的CWnd析構(gòu)函數(shù)會在m_hWnd不為空的情況下調(diào)用DestoryWindow,但這不會導(dǎo)致無窮遞歸,因為此句柄在清除階段將會處于分離狀態(tài)并為空。

            Note   CWnd::PostNcDestroy is normally called after the Windows WM_NCDESTROY message is processed, as part of window destruction, and the HWND and the C++ window object are no longer attached. CWnd::PostNcDestroy will also be called in the implementation of most Create calls if failure occurs (see below for auto cleanup rules).

            注意CWnd::PostNcDestroy一般會在Windows消息WM_NCDESTORY處理后被調(diào)用,把它作為窗口銷毀的一部分,同時HWNDC++窗口對象不再關(guān)聯(lián)。

            CWnd::PostNcDestroy也會在大部分Create調(diào)用的執(zhí)行部分被調(diào)用,如果錯誤發(fā)生的話(自動清理規(guī)則如下)

            Auto Cleanup Classes

            The following classes are not designed for auto-cleanup. They are normally embedded in other C++ object or on the stack:

            • All the standard Windows controls (CStaticCEditCListBox, and so on).
            • Any child windows derived directly from CWnd (for example, custom controls)
            • Splitter windows (CSplitterWnd)
            • Default control bars (classes derived from CControlBar, see Technical Note 31 for enabling auto-delete for control bar objects)
            • Dialogs (CDialog) - designed for modal dialogs on the stack frame
            • All the standard dialogs except CFindReplaceDialog
            • The default dialogs created by ClassWizard

            自動清理類

            以下的這些類不是設(shè)計來做自動清理的。他們通常嵌在其他C++對象或者在堆棧上:

            l         所有的標(biāo)準(zhǔn)Windows控件(CStaticCEditClistBox

            l         所有從CWnd直接派生來的子窗口(比例,自定義控件)

            l         拆分窗口(CSplitterWnd

            l         缺省控制條(從CcontrolBar派生的類,查看TN31來了解能夠自動刪除的控制條對象)

            l         對話框(CDialog)設(shè)計來在堆棧上創(chuàng)建模態(tài)對話框

            l         所有的標(biāo)準(zhǔn)對話框,除了CfindReplaceDialog

            l         ClassWizard創(chuàng)建的缺省對話框

            The following classes are designed for auto-cleanup. They are normally allocated by themselves on the heap:

            • Main frame windows (derived directly or indirectly from CFrameWnd)
            • View windows (derived directly or indirectly from CView)

            以下這些類設(shè)計來做自動清理。他們一般單獨分配在堆上:

            l         主框架窗口(直接或間接派生于CFrameWnd

            l         視圖窗口(直接或間接派生于CView

            If you wish to break any of these rules, you must override the PostNcDestroy member function in your derived class. To add auto-cleanup to your class, simply call your base class and then do a delete this. To remove auto-cleanup from your class, call CWnd::PostNcDestroy directly instead of thePostNcDestroy member in your direct base class.

            如果你想打破任何一條規(guī)則,你就必須在你的派生類中重載PostNcDestroy成員函數(shù)。為了增加自動清理到你的類,只需要調(diào)用你的基類并做delete this操作。為了將自動清理從你的類中移出,直接調(diào)用CWnd::PostNcDestroy來代替你基類的成員函數(shù)PostNcDestory.

            The most common use of the above is to create a modeless dialog that can be allocated on the heap.

            以上內(nèi)容常用在創(chuàng)建一個能在堆上分配的非模態(tài)的對話框

            When to Call 'delete'

            The recommended way to destroy a Windows object is to call DestroyWindow, either the C++ member function or the global ::DestroyWindow API.

            何時調(diào)用delete

            銷毀一個窗口最好是調(diào)用DestoryWindow,不管是C++的成員函數(shù)還是全局的::DestoryWindow API.

            Do not call the global ::DestroyWindow API to destroy an MDI Child window, use the virtual member function CWnd::DestroyWindow instead.

            不要調(diào)用全局的API ::DestroyWindow來銷毀一個MDI子窗口,使用虛擬成員函數(shù)CWnd::DestroyWindow來代替它。

            For C++ Window objects that don't perform auto-cleanup, using DestroyWindow instead of delete avoids problems of having to call DestroyWindow in the CWnd::~CWnd destructor where the VTBL is not pointing to the correctly derived class. This can lead to subtle bugs so the diagnostic (debug) version of MFC will warn you with

            Warning: calling DestroyWindow in CWnd::~CWnd

               OnDestroy or PostNcDestroy in derived class will not be called

            對于那些不執(zhí)行自動清理的C++窗口對象,使用DestoryWindow來代替delete以避免你必須在CWnd::~CWnd析構(gòu)函數(shù)中調(diào)用DestoryWindow的問題,而在此處VTBL并沒有指向正確的派生類。這可能會導(dǎo)致許多bugs,所以MFC診斷版本(調(diào)試)中會警告你:

            Warning: calling DestroyWindow in CWnd::~CWnd

               OnDestroy or PostNcDestroy in derived class will not be called

            In the case of C++ Windows objects that do perform auto-cleanup, you must call DestroyWindow. If you use operator delete directly, the MFC diagnostic memory allocator will alert you that you are freeing memory twice (the first call to delete as well as the indirect call to "delete this" in the auto-cleanup implementation of PostNcDestroy).

            對于執(zhí)行自動清理工作的C++Windows對象,你必須調(diào)用DestroyWindow。如果你直接使用操作符deleteMFC的診斷內(nèi)存分配算符將會警告你:你正在第二次釋放內(nèi)存(第一次調(diào)用delete,還有在PostNcDestroy的自動清理執(zhí)行過程中調(diào)用delete this)。

            After calling DestroyWindow on a non-auto-cleanup object, the C++ object will still be around, butm_hWnd will be NULL. After calling DestroyWindow on an auto-cleanup object, the C++ object will be gone, freed by the C++ delete operator in the auto-cleanup implementation of PostNcDestroy..

            對于一個不執(zhí)行自動清理的對象,在調(diào)用DestroyWindow之后,這個C++對象仍然存在,但是m_hWnd會為空。對一個執(zhí)行自動清理的對象,在調(diào)用DestroyWindow之后,此C++對象就不存在了,它被PostNcDestroy的自動清理執(zhí)行過程里的delete操作符釋放。

            posted on 2010-11-06 14:26 楊粼波 閱讀(1243) 評論(0)  編輯 收藏 引用


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            香蕉99久久国产综合精品宅男自 | 久久国产AVJUST麻豆| 国产毛片久久久久久国产毛片| 国内精品久久久久久久影视麻豆| 中文字幕乱码久久午夜| 国产午夜精品久久久久九九| 久久国产成人| 无码国内精品久久人妻| 手机看片久久高清国产日韩| 久久99国产乱子伦精品免费| 久久伊人五月丁香狠狠色| 99久久无码一区人妻a黑| 日批日出水久久亚洲精品tv| 99999久久久久久亚洲| 久久久久久九九99精品| 久久无码人妻一区二区三区午夜| 久久天天婷婷五月俺也去| 国产精品久久永久免费| 伊人久久大香线蕉av不卡| 日韩精品无码久久一区二区三 | 久久成人18免费网站| 久久精品国产半推半就| 91久久精品91久久性色| 久久久无码精品亚洲日韩蜜臀浪潮 | 日日狠狠久久偷偷色综合0| 久久精品一区二区三区不卡| 久久免费精品视频| 精品久久久久久久无码| 国产成年无码久久久免费| 亚洲伊人久久成综合人影院 | 国产一区二区三精品久久久无广告| 色偷偷久久一区二区三区| 亚洲中文字幕久久精品无码APP| 青青热久久国产久精品 | 久久精品国产亚洲AV电影| 精品国产日韩久久亚洲| 亚洲第一永久AV网站久久精品男人的天堂AV| 国产精品成人久久久久久久| 中文字幕一区二区三区久久网站| 国内精品欧美久久精品| 久久精品这里只有精99品|