开发者

Is there a counterpart to "CreateInstance"?

开发者 https://www.devze.com 2023-02-26 19:45 出处:网络
We have some code that uses MSXML, and does this to create the XML document object: MSXML2::IXMLDOMDocumentPtrdoc_in;

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 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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消