exception during destruction of CComPtr
Asked Answered
S

4

6

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.

Stoker answered 20/7, 2010 at 8:39 Comment(3)
Manual release()s or misuse of other CComPtrs (e.g. through Attach()) could be a reason. If everything else looks fine there could also be memory corruption resulting from other problems.Unquestioning
What is the life time of the object? When is the destructor called?Maxwell
Have you figured out the reason yet? I just encountered a similar problem (except I am using the Debug Interface Access SDK COM classes, but probably the exact com classes used is not the concern). I suspect it's something related to CComPtr implementation bug or whatever.Wen
W
8

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.

Wen answered 4/8, 2011 at 12:36 Comment(1)
An alternative is to put the code declaring an using the CComPtr into a nested block. Another alternative is manually calling CComPtr::Release(). Both will solve exactly the same problem, just a bit differently.Forbidding
B
1

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!

Baribaric answered 15/11, 2010 at 22:52 Comment(0)
T
0

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

m_spXMLDoc.CoCreateInstance(...)
Triad answered 12/8, 2010 at 16:36 Comment(0)
E
0

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.

Eyewash answered 9/11, 2010 at 0:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.