PRB: Resource Leaks in System Components

Last reviewed: July 11, 1997
Article ID: Q171145
The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API) included with: - Microsoft Windows NT versions 3.51, 4.0

SYMPTOMS

If you load and free a system DLL (for example, USER32.DLL, ADVAPI32.DLL, or GDI32.DLL) repeatedly in your Win32 application, a resource leak occurs under Windows NT.

CAUSE

This behavior is by design. The Windows NT system DLLs ADVAPI32.DLL, USER32.DLL, and GDI32.DLL are meant to be loaded only once, that is, when your application is loaded. System DLLs make no attempt to clean up resources when they are unloaded. System DLLs rely on the operating system to clean up all their resources when the associated process terminates.

WORKAROUND

There are two ways to work around this problem:

  • Change the design of your application or DLL so as to not Load/Free System DLLs repeatedly.

-or-
  • If your application's design cannot be changed, then call LoadLibrary on the system DLL before any other actions are performed in your application. Call FreeLibray on the system DLL as the last action before your application terminates. Now, when your application loads and frees the system DLL, only its reference count will be incremented and decremented--no new resources are allocated.


Additional query words: winnt nt
Keywords : BseDLL kbprg kbtshoot
Version : 3.51 4.0
Platform : NT WINDOWS
Issue type : kbprb


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: July 11, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.