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?
COM object lifetime management builds on reference counting via
IUnknowns methodsAddRef()andRelease(). 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/CComQIPtrand_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.