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 开发者_Python百科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 IUnknown
s 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.
If IXMLDOMDocumentPtr is a smart pointer (as it looks like) then it will take care of calling doc_in.Release() for you.
精彩评论