Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the lifetime of a CWnd obtained from CWnd::FromHandle?

According to msdn, when I get a CWnd* with CWnd::FromHandle,

The pointer may be temporary and should not be stored for later use.

What is meant by "later use" is not clear to me. Is it only the scope of the current method? As far as I know, there is no GC in Win32!

like image 918
remio Avatar asked Oct 01 '09 15:10

remio


1 Answers

MFC maintains a number of handle maps, from HWND to CWnd, HDC to CDC etc, which are stored in the thread state. Each handle map contains a permanent map and temporary map - permanent entries are added when you call a method such as CWnd::Create or CDC::Attach, while temporary entries are created when you call FromHandle on a handle that doesn't have a permanent entry.

Temporary entries are cleaned up during idle processing (in CWinApp::OnIdle), so they can only safely be used while processing the current message. As soon as you return to the message loop, or enter another modal loop (e.g. by calling DoModal) then they may be deleted.

like image 69
Phil Devaney Avatar answered Oct 24 '22 19:10

Phil Devaney