Microsoft KB Archive/200292

= FIX: Interfaces Not Released by _com_ptr_t::CreateInstance =

Article ID: 200292

Article Last Modified on 10/24/2003

-

APPLIES TO


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

-



This article was previously published under Q200292



SYMPTOMS
Smart pointers object creation (using _com_ptr_t::CreateInstance ) does not release interface pointers to local or remote servers on failure.



RESOLUTION
For local and remote servers, use the CoCreateInstance API instead of the function, _com_ptr_t::CreateInstance. Then use the smart pointer wrapper to call methods on the object by calling _com_ptr_t::Attach. Once the smart pointer goes out of scope, Release is called for the interface pointer.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

This bug was corrected in Microsoft Visual C++, version 6.0.



MORE INFORMATION
For local and remote servers, call CoCreateInstance directly, as in the following sample code:

HRESULT hr = CoCreateInstance(..., ..., IID_IUnknown, (LPVOID*)&pUnk); if (FAILED(hr)) //Do error handling.

hr = OleRun(pUnk);

if (FAILED(hr)) { pUnk->Release; //Do error handling }

// Query for the required interface, such as IFoo. hr = pUnk->QueryInterface( IID_IFoo, pFoo);

if (FAILED(hr)) { pUnk->Release; //Do error handling }

Once you have the interface, you can use the smart pointer wrapper by calling _com_ptr_t::Attach, as in the following sample code:

// Declare the smart pointer for the IFoo interface. IFooPtr m_pFoo;

// Attach the raw interface pointer which you got above // to the smart pointer wrapper. m_pFoo.Attach(pFoo);

