anand
anand

Reputation: 11339

exception during destruction of CComPtr

I have a member variable declared as

CComPtr<IXMLDOMDocument2> m_spXMLDoc;

XML document is created like this

CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER,
    IID_IXMLDOMDocument2, (void**)&m_spXMLDoc));

Now when application exits, an exception is thrown. Callstack is pointing to p->Release()

~CComPtrBase() throw()
{
   if (p)
      p->Release();
}

When I hover over to p in VS debugger, it points to some valid memory.

The last callstack points to exception in msxm6

msxml6.dll!3d6cXX03() 

Any suggestions, what could be the reason? I don't think it's a CComPtr issue.

Upvotes: 6

Views: 1956

Answers (4)

JavaMan
JavaMan

Reputation: 5054

I had a similar issue and eventually I found that it is was just a bug. I have to make sure that CoUninitialize() is called AFTER the CComPtr is destructed. Otherwise, there will be an exception.

int _tmain(int argc, _TCHAR* argv[]) {
  CoInitialize(NULL);
  mymain(); 
  //put all logic in a separate function so that CComPtr
  //is destructed before CoUninitialize()
  CoUninitialize();
  return 0;
}

Declaring CComPtr in the same function as the CoUninitialize() call will cause the exception since the destruction occurs after the function terminates.

Upvotes: 8

user206705
user206705

Reputation:

Do this before your program exits:

if( m_spXMLDoc.p )
    m_spXMLDoc.Release();

I've seen this before myself. The issue is related with reference counting (obviously), but I've never cared to look for the reason. Hope this helps!

Upvotes: 1

Greg Domjan
Greg Domjan

Reputation: 14115

I'm looking at a similar issue where IExplorer rips the com server for the current web page from under the clients.
The result seems to be that release can't be performed, instead you get com errors like server has disconnected clients.

Upvotes: 0

DanDan
DanDan

Reputation: 10562

You should create the instance using the member functions of CComPtr:

m_spXMLDoc.CoCreateInstance(...)

Upvotes: 0

Related Questions