我的新博客在
http://www.codediscuss.com
http://www.codediscuss.com


http://www.codediscuss.com 是我的博客的新地址。還是以VC++編程為主,有mfc,英語,還有點點shareware的內容。
love in C++, live on MFCto get ready... |
置頂隨筆 #
2007年10月2日 #
2007年1月5日 #
2006年8月9日 #
這個答案本來是作者賣錢的,我忘了是從那個網站下載的了,估計不全。
現在有人要,就放上來吧
http://www.shnenglu.com/Files/flyingxu/ThinkinginCppExercise.rar
(未驗證,可能不全,或者根本就不是答案)
2006年7月26日 #
類似的問題見http://www.experts-exchange.com/Programming/Programming_Languages/MFC/Q_20084406.html
下面的代碼,當用AfxBeginThread開始一個線程時,實際上是有內存泄漏的
2006年7月13日 #
2006年7月11日 #
2006年7月10日 #
csdn上難得的好文章,怕不爭氣的csdn網站過幾天又找不到這篇文章了,我得先轉一下,保存一下:
http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=193097
Q : | MFC返回的臨時對象指針成因? | ||
主要解答者: | QunKangLi | 提交人: | QunKangLi |
感謝: | oldforest、yu_hl、QunKangLi | ||
審核者: | happyparrot | 社區對應貼子: | 查看 |
???? A : | |||
msdn在介紹GetDlgItem的時候,明確指出: ? The ?returned ?pointer ?may ?be ?temporary ?and ?should ?not ?be ?stored ?for ?later ?use. ? 如果真是這樣那我不慘了!在我的一個軟件中,因為常常要使切換按鈕的可用與不可用以及改變按鈕文字,所以我用DetDlgItem把它們存了起來,用的時候直接到數組里面取,這樣的話這個軟件不是犯了一個重大的錯誤了?可是用這么久也沒出錯。 ? 另外,它也是說可能是臨時的,那一定在某種情況下不是臨時的,高手能不能解釋一下這個問題? ? --------------------------------------------------------------- ? // ? ?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里調用AfxUnlockTempMaps,AfxUnlockTempMaps會釋放temporary ?maps. ? ? 所以不要保留GetDlgItem等返回的臨時指針,可以直接保存HWND ?objects,然后CWnd::FromHandle獲取臨時指針來用。 ? ? --------------------------------------------------------------- ? ? 臨時包裝對象會在空閑時間刪除.在同一函數中多次使用是沒用問題的,不同函數中使用先前保存下來的指針就有可能出錯,特別是對話框初使化時候存下來的指針. ? ? ? ? ?在MFC層次上的函數使用CWnd對象,而本地Windows代碼(API)使用句柄.如:當Windows系統調用一個窗口過程時將傳遞一個HWND參數,而MFC本身的消息機制使用CWnd類;為了更好更高效的實現,MFC需要與Windows系統合作,將句柄與CWnd對象進行關聯---它用CHandleMap完成關聯. ? CHandleMap有兩個CMapPtrToPtr的成員變量:m_permanentMap(永久映射表,程序運行過程中對象/句柄之間的關系),m_temporaryMap(臨時映射表,在消息存在的過程中才存在).永久表保存顯式創建的CWnd對象,當對象創建時將在永久目錄下插入一個條目,在CWnd::OnNcDestrory時刪除對應條目.但是Windows有時會傳入某些不是由開發人員顯式創建的窗口的句柄,此時MFC會分配一個臨時對象來包裝這個句柄并將它們的映射保存到臨時映射表中,這些臨時對象會在空閑時間被刪除并移走相應的臨時映射表條目.類似的MFC對象與Windows句柄的映射表有: ? m_pmapHWND: ?? ?? ?? ?? ?? ?? ?? ??窗口句柄與CWnd對象 ? m_pampHMENU: ?? ?? ?? ?? ?? ?? ?? ??菜單句柄與CMenu對象 ? m_pmapHDC: ?? ?? ?? ?? ?? ?? ?? ??設備環境句柄與CDC對象 ? m_pmapHGDIOBJ: ?? ?? ?? ?? ?? ?? ?? ??GDI句柄與CGDI對象 ? m_mapHIMAGELIST: ?? ?? ?? ??圖像鏈表句柄到CImageList對象 ? ? 當給定句柄,如HWND時,MFC簡單調用CWnd* ?PASCAL ?CWnd::FromHandle(HWND ?hWnd), ?此函數內部使用CHandleMap::FromHandle(hWnd)獲取相關聯的CWnd對象.在CHandleMap::FromHandle(h)內部(源代碼在WinHand.cpp),先使用CObject* ?pObject ?= ?LookupPermanent(h); ?? ?? ?? ??if ?(pObject ?!= ?NULL)return ?pObject; ? ?檢查永久表;如永久表中不存在,使用pObject ?= ?LookupTemporary(h)) ?!= ?NULL檢查臨時表,都不存在時,使用pTemp ?= ?= ?m_pClass->CreateObject();if ?(pTemp ?== ?NULL)AfxThrowMemoryException();m_temporaryMap.SetAt((LPVOID)h, ?pTemp);創建臨時對象并將其插入到臨時表中去,同時返回該臨時對象. ? void ?CHandleMap::RemoveHandle(HANDLE ?h)的注釋說明臨時對象將在空閑時由OnIdel釋放: ? ?? ?? ?? ??// ?remove ?only ?from ?permanent ?map ?-- ?temporary ?objects ?are ?removed ? ?? ?? ?? ??// ? ?at ?idle ?in ?CHandleMap::DeleteTemp, ?always! ? 如果想不自動釋放臨時對象,使用void ?AFXAPI ?AfxLockTempMaps()/BOOL ?AFXAPI ?AfxUnlockTempMaps(BOOL ?bDeleteTemps)進行鎖定. ? |
2006年7月9日 #
2006年7月6日 #
2006年6月23日 #