BUG: EMF Playback into DC Causes Memory Leak in GDI

Last reviewed: January 11, 1997
Article ID: Q156696
The information in this article applies to:
  • Microsoft Win32 Software Development Kit (SDK) for Windows 95 4.0

SYMPTOMS

In Windows 95, when calling PlayEnhMetaFile() with a non-NULL clipping region selected into the DC, GDI fails to free a copy of the clipping region it creates, thereby causing a memory leak. This leaked resource is a system resource and will not be freed when the application exits.

CAUSE

When RestoreDC() is called, it copies the hMetaRgn value in the saved block over the hMetaRgn value in the hDC. If the value in the save block is NULL and the value in the hDC is not, the only reference to that region is lost and the region is not freed. Instead, RestoreDC() should check for the above case and free the region.

When PlayEnhMetaFile() is called, it calls the following functions: SaveDC(), SetMetaRgn(), play EMF records, and RestoreDC(). This creates the hMetaRgn if there is a clipping region in the hDC, and then fails to release it.

RESOLUTION

To work around this problem, render the Enhanced Metafile (EMF) unclipped to a bitmap, and BitBlt() the image to the screen.

STATUS

Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

Steps to Reproduce Problem

  1. Take the GEN32 sample application and add code to open an .EMF and play it on the screen 10,000 times, each time with a region selected into the DC:

    for (i= 1 to 10,000) {

           GetClipBox (&rect)
    

           hRegion = CreateRectRgnIndirect (&rect);
           SelectClipRgn (hDC, hRegion);
    

           PlayEnhMetaFile (hDC, hEMF, &rcClient);
    

          SelectClipRgn (hDC, NULL);
          DeleteObject (hRegion);
    
    }

  2. Use any utility to watch the GDI Resources.

  3. Start the modified Gen32 created in step 1. Load the enhanced metafile and draw it 10,000 times. Notice that the gdi resources go down considerably.


KBCategory: kbprg kbbuglist
KBSubcategory: kbnocat
Additional reference words: 4.00 kbdsi



THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 11, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.