Is there a counterpart to "CreateInstance"?

1.4k Views Asked by At

We have some code that uses MSXML, and does this to create the XML document object:

MSXML2::IXMLDOMDocumentPtr  doc_in;

doc_in.CreateInstance("Msxml2.DOMDocument.6.0");

Once we're finished with doc_in, how do we destroy it? Is it just automatically destructed when doc_in goes out of scope, or what?

2

There are 2 best solutions below

0
On BEST ANSWER

COM object lifetime management builds on reference counting via IUnknowns methods AddRef() and Release(). For details see "Using and Implementing IUnknown", in particular "Rules for Managing Reference Counts".

On top of that smart pointers are used, most commonly ATLs CComPtr/CComQIPtr and _com_ptr_t.

So, if you're dealing with a plain pointer to a COM instance, you have to Release() manually to relinquish ownership.
If you have a smart pointer to a COM instance, the Release() should be done for you when the smart pointer instance goes out of scope - but to be sure take a look at the documentation for the actual smart pointer class you are using.

2
On

If IXMLDOMDocumentPtr is a smart pointer (as it looks like) then it will take care of calling doc_in.Release() for you.