BUG: User32.dll Leaks Memory

ID: Q184807

The information in this article applies to:

SYMPTOMS

An increasing amount of memory appears to be consumed by an application that repeatedly loads and unloads the User32.dll. This may be caused by the application or a DLL used by the application that loads and unloads the User32.dll multiple times. If the User32.dll is loading and unloading several times, a message box might appear stating an error occurred.

CAUSE

User32.dll does not free its resources when it is unloaded.

RESOLUTION

Load User32.dll once per application.

STATUS

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

MORE INFORMATION

An application may notice that it is consuming an increasing amount of memory during its execution lifetime even though it does not use any functions that are exported from User32.dll. This may be caused by the application loading and unloading a DLL that uses a function exported from User32.dll.

Steps to Reproduce Behavior

1. Create a DLL that calls a function exported from User32.dll. For

   example:

      #include <windows.h>
      #include <stdio.h>

      BOOL WINAPI DllMain(HINSTANCE hInstance,
                          DWORD dwReason, LPVOID lpReserved )
      {
         if (dwReason == DLL_PROCESS_ATTACH)
            HBRUSH hbr =  GetSysColorBrush( COLOR_BTNFACE );
         else if (dwReason == DLL_PROCESS_DETACH)
         {}
         else if (dwReason == DLL_THREAD_ATTACH)
         {}
         else if (dwReason ==DLL_THREAD_DETACH)
         {}
         return TRUE;
      }

2. Create a console application that loads and unloads this DLL many times.
   For example:

      #include "stdio.h"
      void main (int argc, char **argv)
      {
         int attempt = 0;
         HMODULE hInst;

         while (attempt++ < 2500)
         {
            hInst = LoadLibrary ("..\\TestDLL.dll");
       printf("Attempt %d: DLL Instance %08lX\n",attempt,hInst);

       if (hInst == NULL)
          printf("LoadLibrary failed on attempt %d.\nLast error
                       reports : %d\n", attempt,GetLastError());
            else
          FreeLibrary(hInst);
         }
      }

REFERENCES

For additional information, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q171145
   TITLE     : PRB: Resource Leaks in System Components

Additional query words: GDI MFC
Keywords          : kbAPI kbDLL kbKernBase kbPerfMon kbGrpKernBase 
Version           : WIN95; WINNT:4.0
Platform          : Win95 winnt
Issue type        : kbbug
Solution Type     : kbpending

Last Reviewed: August 7, 1998