• <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>

            love in C++, live on MFC

            to get ready...

            C++博客 首頁(yè) 新隨筆 聯(lián)系 聚合 管理
              47 Posts :: 0 Stories :: 97 Comments :: 0 Trackbacks

            csdn上難得的好文章,怕不爭(zhēng)氣的csdn網(wǎng)站過幾天又找不到這篇文章了,我得先轉(zhuǎn)一下,保存一下:
            http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=193097

            Q : MFC返回的臨時(shí)對(duì)象指針成因?
            主要解答者: QunKangLi 提交人: QunKangLi
            感謝: oldforest、yu_hl、QunKangLi
            審核者: happyparrot 社區(qū)對(duì)應(yīng)貼子: 查看
            ???? A :

            msdn在介紹GetDlgItem的時(shí)候,明確指出: ?
            The ?returned ?pointer ?may ?be ?temporary ?and ?should ?not ?be ?stored ?for ?later ?use. ?
            如果真是這樣那我不慘了!在我的一個(gè)軟件中,因?yàn)槌3R骨袚Q按鈕的可用與不可用以及改變按鈕文字,所以我用DetDlgItem把它們存了起來,用的時(shí)候直接到數(shù)組里面取,這樣的話這個(gè)軟件不是犯了一個(gè)重大的錯(cuò)誤了?可是用這么久也沒出錯(cuò)。 ?
            另外,它也是說可能是臨時(shí)的,那一定在某種情況下不是臨時(shí)的,高手能不能解釋一下這個(gè)問題? ?
            --------------------------------------------------------------- ?
            // ? ?Most ?Windows ?objects ?are ?represented ?with ?a ?HANDLE, ?including ?
            // ? ? ? ? ? ?the ?most ?important ?ones, ?HWND, ?HDC, ?HPEN, ?HFONT ?etc. ?
            // ? ?We ?want ?C++ ?objects ?to ?wrap ?these ?handle ?based ?objects ?whenever ?we ?can. ?
            // ? ?Since ?Windows ?objects ?can ?be ?created ?outside ?of ?C++ ?(eg: ?calling ?
            // ? ? ? ? ? ?::CreateWindow ?will ?return ?an ?HWND ?with ?no ?C++ ?wrapper) ?we ?must ?
            // ? ? ? ? ? ?support ?a ?reasonably ?uniform ?mapping ?from ?permanent ?handles ?
            // ? ? ? ? ? ?(i.e. ?the ?ones ?allocated ?in ?C++) ?and ?temporary ?handles ?(i.e. ?
            // ? ? ? ? ? ?the ?ones ?allocated ?in ?C, ?but ?passed ?through ?a ?C++ ?interface. ?
            // ? ?We ?keep ?two ?dictionaries ?for ?this ?purpose. ? ?The ?permanent ?dictionary ?
            // ? ? ? ? ? ?stores ?those ?C++ ?objects ?that ?have ?been ?explicitly ?created ?by ?
            // ? ? ? ? ? ?the ?developer. ? ?The ?C++ ?constructor ?for ?the ?wrapper ?class ?will ?
            // ? ? ? ? ? ?insert ?the ?mapping ?into ?the ?permanent ?dictionary ?and ?the ?C++ ?
            // ? ? ? ? ? ?destructor ?will ?remove ?it ?and ?possibly ?free ?up ?the ?associated ?
            // ? ? ? ? ? ?Windows ?object. ?
            // ? ?When ?a ?handle ?passes ?through ?a ?C++ ?interface ?that ?doesn't ?exist ?in ?
            // ? ? ? ? ? ?the ?permanent ?dictionary, ?we ?allocate ?a ?temporary ?wrapping ?object ?
            // ? ? ? ? ? ?and ?store ?that ?mapping ?into ?the ?temporary ?dictionary. ?
            // ? ?At ?idle ?time ?the ?temporary ?wrapping ?objects ?are ?flushed ?(since ?you ?better ?
            // ? ? ? ? ? ?not ?be ?holding ?onto ?something ?you ?didn't ?create). ?
            // ?
            ?
            ?
            在CWinThread::OnIdle里調(diào)用AfxUnlockTempMaps,AfxUnlockTempMaps會(huì)釋放temporary ?maps. ?
            ?
            所以不要保留GetDlgItem等返回的臨時(shí)指針,可以直接保存HWND ?objects,然后CWnd::FromHandle獲取臨時(shí)指針來用。 ?
            ?
            --------------------------------------------------------------- ?
            ?
            臨時(shí)包裝對(duì)象會(huì)在空閑時(shí)間刪除.在同一函數(shù)中多次使用是沒用問題的,不同函數(shù)中使用先前保存下來的指針就有可能出錯(cuò),特別是對(duì)話框初使化時(shí)候存下來的指針. ?
            ? ? ? ?在MFC層次上的函數(shù)使用CWnd對(duì)象,而本地Windows代碼(API)使用句柄.如:當(dāng)Windows系統(tǒng)調(diào)用一個(gè)窗口過程時(shí)將傳遞一個(gè)HWND參數(shù),而MFC本身的消息機(jī)制使用CWnd類;為了更好更高效的實(shí)現(xiàn),MFC需要與Windows系統(tǒng)合作,將句柄與CWnd對(duì)象進(jìn)行關(guān)聯(lián)---它用CHandleMap完成關(guān)聯(lián). ?
            CHandleMap有兩個(gè)CMapPtrToPtr的成員變量:m_permanentMap(永久映射表,程序運(yùn)行過程中對(duì)象/句柄之間的關(guān)系),m_temporaryMap(臨時(shí)映射表,在消息存在的過程中才存在).永久表保存顯式創(chuàng)建的CWnd對(duì)象,當(dāng)對(duì)象創(chuàng)建時(shí)將在永久目錄下插入一個(gè)條目,在CWnd::OnNcDestrory時(shí)刪除對(duì)應(yīng)條目.但是Windows有時(shí)會(huì)傳入某些不是由開發(fā)人員顯式創(chuàng)建的窗口的句柄,此時(shí)MFC會(huì)分配一個(gè)臨時(shí)對(duì)象來包裝這個(gè)句柄并將它們的映射保存到臨時(shí)映射表中,這些臨時(shí)對(duì)象會(huì)在空閑時(shí)間被刪除并移走相應(yīng)的臨時(shí)映射表?xiàng)l目.類似的MFC對(duì)象與Windows句柄的映射表有: ?
            m_pmapHWND: ?? ?? ?? ?? ?? ?? ?? ??窗口句柄與CWnd對(duì)象 ?
            m_pampHMENU: ?? ?? ?? ?? ?? ?? ?? ??菜單句柄與CMenu對(duì)象 ?
            m_pmapHDC: ?? ?? ?? ?? ?? ?? ?? ??設(shè)備環(huán)境句柄與CDC對(duì)象 ?
            m_pmapHGDIOBJ: ?? ?? ?? ?? ?? ?? ?? ??GDI句柄與CGDI對(duì)象 ?
            m_mapHIMAGELIST: ?? ?? ?? ??圖像鏈表句柄到CImageList對(duì)象 ?
            ?
            當(dāng)給定句柄,如HWND時(shí),MFC簡(jiǎn)單調(diào)用CWnd* ?PASCAL ?CWnd::FromHandle(HWND ?hWnd), ?此函數(shù)內(nèi)部使用CHandleMap::FromHandle(hWnd)獲取相關(guān)聯(lián)的CWnd對(duì)象.在CHandleMap::FromHandle(h)內(nèi)部(源代碼在WinHand.cpp),先使用CObject* ?pObject ?= ?LookupPermanent(h); ?? ?? ?? ??if ?(pObject ?!= ?NULL)return ?pObject; ? ?檢查永久表;如永久表中不存在,使用pObject ?= ?LookupTemporary(h)) ?!= ?NULL檢查臨時(shí)表,都不存在時(shí),使用pTemp ?= ?= ?m_pClass->CreateObject();if ?(pTemp ?== ?NULL)AfxThrowMemoryException();m_temporaryMap.SetAt((LPVOID)h, ?pTemp);創(chuàng)建臨時(shí)對(duì)象并將其插入到臨時(shí)表中去,同時(shí)返回該臨時(shí)對(duì)象. ?
            void ?CHandleMap::RemoveHandle(HANDLE ?h)的注釋說明臨時(shí)對(duì)象將在空閑時(shí)由OnIdel釋放: ?
            ?? ?? ?? ??// ?remove ?only ?from ?permanent ?map ?-- ?temporary ?objects ?are ?removed ?
            ?? ?? ?? ??// ? ?at ?idle ?in ?CHandleMap::DeleteTemp, ?always! ?
            如果想不自動(dòng)釋放臨時(shí)對(duì)象,使用void ?AFXAPI ?AfxLockTempMaps()/BOOL ?AFXAPI ?AfxUnlockTempMaps(BOOL ?bDeleteTemps)進(jìn)行鎖定. ?



            posted on 2006-07-10 23:18 flyingxu 閱讀(1725) 評(píng)論(1)  編輯 收藏 引用 所屬分類: VC/MFC

            Feedback

            # re: [zz]MFC返回的臨時(shí)對(duì)象指針成因? 2008-06-04 16:43 Touchsoft
            csdn上難得的好文章,怕不爭(zhēng)氣的csdn網(wǎng)站過幾天又找不到這篇文章了,我得先轉(zhuǎn)一下,保存一下:

            我直接保存你的這篇到硬盤了 :)


            如果真是這樣那我不慘了!在我的一個(gè)軟件中,因?yàn)槌3R骨袚Q按鈕的可用與不可用以及改變按鈕文字,所以我用DetDlgItem把它們存了起來,用的時(shí)候直接到數(shù)組里面取,這樣的話這個(gè)軟件不是犯了一個(gè)重大的錯(cuò)誤了?可是用這么久也沒出錯(cuò)。

            我曾經(jīng)也這樣用,不過很遺憾,我發(fā)覺我獲取的CCombox添加的數(shù)據(jù)失敗了,后來沒辦法,就定義控件成員變量,看到這篇文章,所以收藏。

            我沒看GetDlgItem的MSDN描述,一直相信它可以獲得“正確”的“穩(wěn)定”的指針。哎~  回復(fù)  更多評(píng)論
              

            久久久久人妻一区二区三区| 久久青青草原精品国产不卡| 久久精品免费全国观看国产| 久久91精品国产91| 久久精品人人做人人妻人人玩| 99久久er这里只有精品18| 一本久久a久久精品综合夜夜| 久久精品国产亚洲Aⅴ香蕉 | 国内精品久久久久影院日本| 精品国产乱码久久久久久1区2区 | 久久中文字幕视频、最近更新| 久久久久综合中文字幕| 亚洲午夜久久久久妓女影院| 国内精品久久久久国产盗摄| 中文字幕乱码人妻无码久久| 国产精品无码久久综合网| 亚洲中文字幕无码久久2020| 国产精品成人99久久久久 | 久久久精品免费国产四虎| 欧美午夜A∨大片久久 | 精品国产一区二区三区久久久狼| 久久久WWW成人| 亚洲国产成人久久综合碰碰动漫3d| 国产69精品久久久久观看软件| 亚洲综合久久综合激情久久| 久久婷婷五月综合色奶水99啪| 天天综合久久一二三区| 国产巨作麻豆欧美亚洲综合久久| 精品国际久久久久999波多野| 99久久精品免费看国产一区二区三区| 91精品久久久久久无码| 国产精品久久久亚洲| 久久久久久精品免费免费自慰| 欧美日韩中文字幕久久久不卡 | 久久丫精品国产亚洲av不卡| 久久精品卫校国产小美女| 青青久久精品国产免费看| 久久综合久久鬼色| 亚洲欧美国产精品专区久久| 国产精品久久婷婷六月丁香| 香蕉aa三级久久毛片|