BUG: Using #import May Cause Memory Leaks

ID: Q231872


The information in this article applies to:
  • Microsoft Visual C++, 32-bit Enterprise Edition, versions 5.0, 6.0


SYMPTOMS

When using #import to generate wrapper classes for COM objects, _com_dispatch_method(), _com_dispatch_raw_method(), _com_dispatch_propput(), _com_dispatch_raw_propput(), _com_dispatch_propget(), and _com_dispatch_raw_propget() function calls may be generated.

These functions leak memory if an exception is thrown by the COM object. Specifically, ICreateErrorInfo is not released and three BSTRs are not freed.


CAUSE

This is due to a bug in the internal implementation of the compiler native COM support.


RESOLUTION

A supported fix that corrects this problem is now available from Microsoft.

To resolve this problem immediately, contact Microsoft Product Support Services to obtain the fix. For a complete list of Microsoft Product Support Services phone numbers and information on support costs, please go to the following address on the World Wide Web:

http://support.microsoft.com/support/supportnet/default.asp

The English version of this fix should have the following file attributes or later:


   Date        Time      Size        File Name    Version#   Platform
   -------------------------------------------------------------------
   06/04/1999  11:14PM   184KB       COMSUPP.LIB  N/A        (x86) 


NOTE: If this product was already installed on your computer when you purchased it from the Original Equipment Manufacturer (OEM) and you need this fix, please call the Pay Per Incident number listed on the above Web site. If you contact Microsoft to obtain this fix, and if it is determined that you only require the fix you requested, no fee will be charged. However, if you request additional technical support, and if your no-charge technical support period has expired, or if you are not eligible for standard no-charge technical support, you may be charged a non-refundable fee. For more information about eligibility for no-charge technical support, see the following article in the Microsoft Knowledge Base: Q154871 Determining If You Are Eligible for No-Charge Technical Support


STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.


MORE INFORMATION

Steps to Reproduce Behavior

  1. Create a Visual Basic COM server. You can choose ActiveX DLL.


  2. Name the project LeakServer.


  3. Name the class LeakTester.


  4. Add one Public Sub called Leak to the class. For example:
    
           Public Sub Leak()
    
           End Sub 


  5. Add the following code to the Leak Subroutine:
    
             Err.Raise vbObjectError + &H200 + 5 


  6. Build the ActiveX DLL. When prompted, place the DLL on C drive.


  7. Create a new MFC AppWizard-generated dialog box application. Name it LeakTest.


  8. Add the following line to the Stdafx.h file:


  9. 
         #import "C:\LeakServer.dll" no_namespace no_dual_interfaces 
  10. Add a button and use Class Wizard to add a BN_CLICK handler.


  11. Add the following code to the handler:
    
      _LeakTesterPtr serv;
      HRESULT hr = serv.CreateInstance(__uuidof(LeakTester));
      if (FAILED(hr))
      {
          TRACE("Create failed= %x\n", hr);
          return;
      }
    
      int nResponse = AfxMessageBox ("Catch 10 _com_errors?", MB_YESNO);
      while (IDYES == nResponse)
      {
         for (int iLoop = 0; iLoop < 10; iLoop++)
         {
            try
            {
              serv->Leak();
            }
            catch(_com_error &x)
            {
              int i = 0;
            }
        }
        nResponse = AfxMessageBox ("Catch 10 _com_errors?", MB_YESNO);
      }
     


  12. Add the following code before the button handler:
    
    struct InitCOM
    {
      InitCOM()  { CoInitialize(NULL); }
      ~InitCOM() { CoUninitialize();   }
    } g_Init; 


  13. Build and test.


You can use Task Manager in Windows NT to see the memory increase through each iteration.

Additional query words: leak _com_ptr_t _com_error native COM #import kbqfe

Keywords : kbCOMt kbLangCPP kbVC600bug kbVC600QFE kbSmartPtr
Version : WINDOWS:5.0,6.0
Platform : WINDOWS
Issue type : kbbug


Last Reviewed: November 8, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.