Microsoft KB Archive/839859

= Temporary MFC objects may not be deleted in Windows CE Platform Builder =

Article ID: 839859

Article Last Modified on 8/18/2005

-

APPLIES TO


 * Microsoft Windows CE Platform Builder 2.11
 * Microsoft Windows CE Platform Builder 2.12
 * Microsoft Encarta Reference Suite 2001
 * Microsoft Windows CE Platform Builder 4.0
 * Microsoft Windows CE Platform Builder 4.1
 * Microsoft Windows CE Platform Builder 4.2

-





SYMPTOMS
Temporary Microsoft Foundation Class (MFC) objects may not be deleted in Microsoft Windows CE Platform Builder. MFC applications may leak memory. A Windows CE Platform Builder application that uses the MFC object may consume an increasing amount of memory as window objects are used.



CAUSE
MFC creates a temporary mapping of a Win32 system object handle to an MFC object when a Win32 system object handle is used in an MFC application. The temporary mapping is performed for the following Win32 system object handles:
 * Window handles, such as dialog boxes
 * Image lists
 * Graphics device interface (GDI) objects
 * Menus
 * Device contexts (DCs)

Note Windows CE Platform Builder does not implement the type of idle processing that Microsoft Desktop Windows MFC uses. Desktop Windows MFC uses idle processing time to clean up the memory that is used by these temporary objects. Windows CE Platform Builder does not do this. Therefore, the temporary objects are never deleted until the application exits.



WORKAROUND
To work around this problem, each MFC application must contain a call to the CWinApp::OnIdle method. You can use the following code example to do this. AfxGetApp->CWinApp::OnIdle(1); This call must be at a location in the program flow that is reached at set intervals. However, the location in the program flow must be where none of the affected temporary objects are in scope or are instantiated. For example, temporary objects are not in scope and are not instantiated when the following conditions are true:
 * When you select a main menu item
 * After a dialog box is closed

The CWinApp::OnIdle method deletes the temporary mapping for all base classes. Temporary objects of a specific base class may be deleted by calling the DeleteTempMap method of the object. You can use the following code example to do this.

CDC::DeleteTempMap; Warning Do not delete the temporary map while the object is still in scope or is still instantiated. This may cause a different memory leak to occur.

