I wish to share with you this problem I'm facing. Long story short, I have this little code (test purpose only):
int main ()
{
IXMLDOMDocument *pDoc(nullptr);
CoCreateInstance(CLSID_DOMDocument, nullptr, CLSCTX_ALL, IID_IXMLDOMDocument, reinterpret_cast<LPVOID*>(&pDoc));
DWORD d = pDoc->AddRef();
std::cout << "pDoc: add ptr=" << pDoc << " d=" << d << std::endl;
d = pDoc->Release();
std::cout << "pDoc: rel ptr=" << pDoc << " d=" << d << std::endl;
IUnknown *pUnk(nullptr);
pDoc->QueryInterface(IID_IUnknown, reinterpret_cast<LPVOID*>(&pUnk));
d = pUnk->AddRef();
std::cout << "pUnk: add ptr=" << pUnk << " d=" << d << std::endl;
d = pUnk->Release();
std::cout << "pUnk: rel ptr=" << pUnk << " d=" << d << std::endl;
/*Release objects*/
d = pUnk->Release();
std::cout << "pUnk: rel ptr=" << pUnk << " d=" << d << std::endl;
d = pDoc->Release();
std::cout << "pDoc: rel ptr=" << pDoc << " d=" << d << std::endl;
return 0;
}
I'm expecting that the last 2 cout
print 0 as the returned count, in their place I'm seeing:
pDoc: add ptr=004A4628 d=2
pDoc: rel ptr=004A4628 d=1
pUnk: add ptr=004A3A10 d=4
pUnk: rel ptr=004A3A10 d=3
pUnk: rel ptr=004A3A10 d=2
pDoc: rel ptr=004A4628 d=0
Why QueryInterface
returned me an IUnknown
which internal count begins in 3?
Why last Release
method of the IUnknown
object isn't returning 0
as excepted?
What I'm might be missing?
According to the MDSN documention of AddRef
and Release
, the return value is intended to only be used for test purposes. It may not be an accurate reflection of the actual number of references; and in particular testing it against 0
is not a guarantee that the object is finished.
Under what conditions will the IUnknown::AddRef method return 0?.