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)行鎖定. ?
|