青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

牽著老婆滿街逛

嚴(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(銷毀窗口對(duì)象)

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

TN017: Destroying Window Objects

銷毀窗口對(duì)象

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

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

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

問(wèn)題

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

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

銷毀窗口

有兩種方法被允許來(lái)銷毀一個(gè)Windows對(duì)象:

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

l         利用delete操作符來(lái)進(jìn)行明確的刪除工作。

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

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進(jìn)行自動(dòng)清除

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

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的缺省操作是什么也不做,這適合于那些分配在堆棧或者嵌在其他對(duì)象里面的窗口對(duì)象。這不適合于那些設(shè)計(jì)來(lái)分配在堆上的窗口對(duì)象(不嵌在其他C++對(duì)象中)。

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è)計(jì)來(lái)分配在堆上的類可以重載成員函數(shù)PostNcDestroy以執(zhí)行“delete this”操作。它將會(huì)釋放任何與此C++對(duì)象相關(guān)的C++內(nèi)存。盡管缺省的CWnd析構(gòu)函數(shù)會(huì)在m_hWnd不為空的情況下調(diào)用DestoryWindow,但這不會(huì)導(dǎo)致無(wú)窮遞歸,因?yàn)榇司浔谇宄A段將會(huì)處于分離狀態(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一般會(huì)在Windows消息WM_NCDESTORY處理后被調(diào)用,把它作為窗口銷毀的一部分,同時(shí)HWNDC++窗口對(duì)象不再關(guān)聯(lián)。

CWnd::PostNcDestroy也會(huì)在大部分Create調(diào)用的執(zhí)行部分被調(diào)用,如果錯(cuò)誤發(fā)生的話(自動(dòng)清理規(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

自動(dòng)清理類

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

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

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

l         拆分窗口(CSplitterWnd

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

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

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

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

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è)計(jì)來(lái)做自動(dòng)清理。他們一般單獨(dú)分配在堆上:

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ù)。為了增加自動(dòng)清理到你的類,只需要調(diào)用你的基類并做delete this操作。為了將自動(dòng)清理從你的類中移出,直接調(diào)用CWnd::PostNcDestroy來(lái)代替你基類的成員函數(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)建一個(gè)能在堆上分配的非模態(tài)的對(duì)話框

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.

何時(shí)調(diào)用delete

銷毀一個(gè)窗口最好是調(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來(lái)銷毀一個(gè)MDI子窗口,使用虛擬成員函數(shù)CWnd::DestroyWindow來(lái)代替它。

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

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

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).

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

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

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


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


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            亚洲视频一区| 久久在线视频在线| 久久躁日日躁aaaaxxxx| 翔田千里一区二区| 欧美在线观看视频在线| 久久精品在线播放| 免费一级欧美片在线观看| 欧美华人在线视频| 99国产精品久久久久久久| 一本色道久久综合| 欧美一站二站| 欧美成人一区二区三区| 国产精品久久7| 樱桃国产成人精品视频| 日韩一区二区免费高清| 性欧美xxxx视频在线观看| 久久视频在线看| 亚洲精品日韩久久| 欧美一级成年大片在线观看| 久久婷婷人人澡人人喊人人爽| 欧美高清在线精品一区| 欧美xart系列高清| 国产精品一区二区在线观看| 欧美视频一区在线观看| 国产日产精品一区二区三区四区的观看方式 | 夜夜嗨一区二区| 性色av香蕉一区二区| 欧美成人在线免费视频| 国产精品日韩电影| 亚洲国产婷婷综合在线精品| 国内精品久久久久久 | 午夜精品成人在线视频| 免费成人激情视频| 亚洲婷婷综合色高清在线| 久久深夜福利| 国产精品一区久久久久| 亚洲精品一区二区三区蜜桃久| 午夜久久久久久| 亚洲精品免费看| 亚洲午夜久久久久久久久电影院 | 午夜精品在线观看| 亚洲国产美国国产综合一区二区| 午夜欧美精品| 国产精品久久久久久av福利软件| 亚洲麻豆av| 欧美第一黄网免费网站| 久久久国产午夜精品| 国产伦精品一区二区三区| 亚洲一区二区三区高清| 亚洲欧洲日产国产网站| 久久免费黄色| 在线播放亚洲| 免费在线播放第一区高清av| 亚洲欧美日韩在线播放| 国产精品爽爽爽| 新狼窝色av性久久久久久| 亚洲图片在线观看| 国产精品入口尤物| 久久gogo国模裸体人体| 亚洲欧美日本精品| 国产欧美一区二区三区视频| 欧美一区二区三区免费大片| 亚洲综合999| 韩国一区二区三区美女美女秀| 久久精品国产一区二区三区免费看| 亚洲一区国产一区| 国产日韩欧美高清免费| 久久久久综合| 久久婷婷国产综合国色天香| 91久久精品网| 亚洲免费激情| 国内精品模特av私拍在线观看| 久久精品九九| 欧美在线观看日本一区| 性欧美video另类hd性玩具| 国产精品亚发布| 欧美中文在线免费| 久久只有精品| 99re热这里只有精品免费视频| 亚洲精品久久久久久久久久久久久| 欧美精品日韩综合在线| 亚洲欧美日韩一区二区| 羞羞色国产精品| 亚洲国产另类久久精品| 亚洲精品一二区| 国产日产欧美a一级在线| 蜜臀av国产精品久久久久| 欧美极品aⅴ影院| 亚洲性夜色噜噜噜7777| 亚欧成人在线| 夜夜狂射影院欧美极品| 午夜精品久久久久久久久久久| 一区二区亚洲精品| 一个色综合导航| 一色屋精品视频免费看| 日韩午夜av| 在线不卡视频| 亚洲视屏在线播放| 亚洲国产成人91精品| 国产精品99久久久久久白浆小说| 伊人精品在线| 亚洲欧美日韩国产成人| 亚洲高清视频在线| 午夜精品剧场| 亚洲性视频网址| 欧美成人午夜激情视频| 久久精品论坛| 国产精品白丝黑袜喷水久久久| 免费欧美在线视频| 国产亚洲精品久久久久久| 亚洲人被黑人高潮完整版| 国外成人在线视频网站| 亚洲一区免费视频| 亚洲视频一区二区| 欧美电影免费观看高清| 久久字幕精品一区| 国产区在线观看成人精品| 一区二区激情| 在线一区二区三区做爰视频网站 | 亚洲一区二区三区在线看| 99国产精品久久久久久久| 久久中文久久字幕| 久热精品视频在线观看| 国产日韩一区二区| 这里只有精品视频| 中文日韩在线| 欧美日韩日本国产亚洲在线| 亚洲第一精品福利| 亚洲国产精品福利| 久久精品成人一区二区三区蜜臀| 性18欧美另类| 国产视频久久久久久久| 亚洲伊人一本大道中文字幕| 亚洲图片欧洲图片av| 欧美日韩性生活视频| 亚洲午夜未删减在线观看| 亚洲美女黄网| 91久久极品少妇xxxxⅹ软件| 久久精品国产精品亚洲| 性做久久久久久免费观看欧美| 国产精品hd| 亚洲午夜久久久| 久久精品国语| 亚洲国产99精品国自产| 女同性一区二区三区人了人一| 欧美成人免费网| 日韩视频免费观看高清完整版| 欧美激情一区三区| 亚洲精品视频在线观看免费| 在线亚洲激情| 国产精品女人网站| 欧美一区二区| 亚洲成色www久久网站| 99这里有精品| 国产精品日日摸夜夜摸av| 亚洲欧美中文另类| 免费观看久久久4p| 99热在这里有精品免费| 国产精品高潮呻吟久久av无限| 亚洲男同1069视频| 欧美jizz19hd性欧美| 一本久久综合亚洲鲁鲁| 国产欧美在线| 免费一级欧美在线大片| 一区二区三区视频在线观看| 久久精品亚洲精品| 亚洲欧洲综合另类| 国产精品视频精品| 久热精品在线视频| 亚洲一区二区精品在线观看| 麻豆精品传媒视频| 亚洲一区二区在线免费观看视频| 很黄很黄激情成人| 欧美性大战xxxxx久久久| 久久天堂av综合合色| 制服丝袜亚洲播放| 欧美激情亚洲| 久久精品视频在线播放| 中国亚洲黄色| 亚洲国产成人不卡| 国产欧美精品国产国产专区| 欧美大片在线观看一区| 欧美专区18| 亚洲男人的天堂在线aⅴ视频| 欧美激情综合色| 久久久国产一区二区三区| 中文欧美日韩| 日韩一二三在线视频播| 在线成人激情黄色| 国产欧美日韩一区二区三区| 欧美精品综合| 欧美/亚洲一区| 久久精视频免费在线久久完整在线看 | 老司机成人在线视频| 亚洲综合色丁香婷婷六月图片| 亚洲大胆女人| 麻豆精品在线视频| 久久久久久午夜| 欧美在线一二三四区| 亚洲一区二区三区四区中文|