Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

Unloading a MFC DLL created using Visual C++ 2008 Service Pack 1 causes a memory leak


Symptoms

When the LoadLibrary function loads a MFC DLL created using Visual C++ 2008 Service Pack 1 (SP1) and then the FreeLibrary function unloads it, a memory leak occurs.

↑ Back to the top


Cause

This is caused because an internal object created when the MFC DLL is loaded is not destroyed when the DLL is unloaded.

↑ Back to the top


More Information

Status:
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.

Steps to reproduce the problem:

You can reproduce this problem with the MFC DLL created using the following steps.

1. Start Visual C++ 2008 Service Pack 1.

2. Create a MFC DLL following the steps below:

(1) From the menu, choose File -> New -> Project to open the New Project dialog.
(2) In the Project Types list, do (A) or (B) below, and in the Name field, type a project name.

  (A) Choose Visual C++ -> MFC -> MFC DLL Application
  (B) Choose Visual C++ -> Win32 -> Win32 Console Application

(3) In the application wizard dialog, click the Application Settings page and set either of the following settings depending on the action you did in the step (2). Click the Finish button.

  (A) [Visual C++] - [MFC] - [MFC DLL Application]
    You should have chosen either of the following options in the Type of DLL:
      - Regular DLL using shared MFC DLL
      - Regular DLL with MFC statically linked

  (B) [Visual C++] - [Win32] - [Win32 Console Application]
    - Application type: Check the DLL checkbox.
    - Add common header files for: Check the MFC checkbox.
    - Additional options: Check the Precompiled header checkbox.

3. Again, choose File -> New -> Project to open the New Project dialog. In the New Project dialog, choose Visual C++ -> Win32 -> Win32 Console Application to create a new project. In the new project, use the LoadLibrary function to load the MFC DLL that you just created in the step 2 and then use the FreeLibrary function to unload it. This causes a memory leak.

Code Example:

 #include "stdafx.h"
 #include <windows.h>

 int _tmain(int argc, _TCHAR* argv[])
 {
  HINSTANCE hinstLib = LoadLibrary(_T("MFCDLL.dll"));

  FreeLibrary(hinstLib);

  return 0;
 }

↑ Back to the top


Keywords: kb

↑ Back to the top

Article Info
Article ID : 2446414
Revision : 1
Created on : 1/7/2017
Published on : 10/28/2010
Exists online : False
Views : 130