Microsoft KB Archive/231872

= Functions may leak memory if an exception is thrown by the COM object when you use #import to generate wrapper classes for COM objects =

Article ID: 231872

Article Last Modified on 9/23/2005

-

APPLIES TO


 * Microsoft Visual C++ 5.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Enterprise Edition

-



This article was previously published under Q231872



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/default.aspx?scid=fh;EN-US;CNTACTMS

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) 08/05/2000 01:55PM   198KB       COMSUPP.LIB  N/A        (alpha)



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



Steps to reproduce behavior
 Create a Visual Basic COM server. You can choose ActiveX DLL. Name the project LeakServer. Name the class LeakTester.  Add one Public Sub called Leak to the class. For example: Public Sub Leak

End Sub   Add the following code to the Leak Subroutine: Err.Raise vbObjectError + &H200 + 5 </li> Build the ActiveX DLL. When prompted, place the DLL on C drive.</li> Create a new MFC AppWizard-generated dialog box application. Name it LeakTest.</li>  Add the following line to the Stdafx.h file: #import "C:\LeakServer.dll" no_namespace no_dual_interfaces </li> Add a button and use Class Wizard to add a BN_CLICK handler.</li>  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); }

</li>  Add the following code before the button handler: struct InitCOM { InitCOM  { CoInitialize(NULL); } ~InitCOM { CoUninitialize;  } } g_Init; </li> Build and test.</li></ol>

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

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

Keywords: kbbug kbfix kbvs600sp4fix kbvs600sp5fix kblangcpp kbqfe kbprogramming kbsmartptr kbactivex kbhotfixserver KB231872

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.