FIX: Access Violation After Unloading Extension DLL

Last reviewed: September 19, 1997
Article ID: Q147315
The information in this article applies to:
  • The AppWizard included with: - Microsoft Visual C++, 32-bit Edition, versions 2.0, 2.1, 2.2, 4.0,

         4.1, 4.2
    

SYMPTOMS

After you dynamically unload an AppWizard-generated Microsoft Foundation Class (MFC) Extension DLL, an access violation may occur as you try to gain access to Windows resources. The last MFC function on the call stack is usually AfxFindResourceHandle().

NOTE: This occurs only when Extension DLLs are loaded and unloaded explicitly with calls to AfxLoadLibrary() and AfxFreeLibrary() or LoadLibrary() and FreeLibrary(). Most Extension DLLs are loaded implicitly by linking to their import libraries.

CAUSE

When AppWizard generates the program template for an Extension DLL, it is not including a call to AfxTermExtensionModule(). This function removes the DLL from the list of loaded MFC Extension DLLs. Since this list is used when searching for resources, an access violation may occur after an AppWizard-generated Extension DLL is freed.

RESOLUTION

Add the following line to the DLL_PROCESS_DETACH section of an AppWizard- generated Extension DLL's DllMain():

   else if (dwReason == DLL_PROCESS_DETACH)
   {
      // Add the following line
      AfxTermExtensionModule(extensionDLL);

          // TODO: perform other cleanup tasks here
   }

extensionDLL should be the same static value passed to AfxInitExtensionModule() in the DLL_PROCESS_ATTACH section. This value should be different for every Extension DLL.

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This problem has been fixed in Visual C++ 5.0.

MORE INFORMATION

NOTE: Dynamically loading and unloading MFC Extension DLLs was not supported in versions prior to Visual C++ 4.0. Dynamically loading or freeing Extension DLLs built with earlier versions could corrupt static MFC state information.

AfxLoadLibrary() and AfxFreeLibrary() should be called to load and free MFC Extension DLLs. Calling the Win32 API functions LoadLibrary() and FreeLibrary() to load or free MFC Extension DLLs can cause problems in multi-threaded applications.

For more information on AfxTermExtensionModule() see the "TN033: DLL Version of MFC" technote in the MFC documentation.


Additional query words: _AFXDLL gpfault gpf


Keywords : MfcDLL vcfixlist500 WizardIss kbtool kbbuglist
Technology : kbMfc
Version : 2.0 2.1 2.2 4.0 4.1 4.2
Platform : NT WINDOWS
Issue type : kbbug
Solution Type : kbfix


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