2008年6月30日星期一

[COM]-ruler of ref count

1. Addref before return. Call Addref if function return interface point.(QueryInterface, CreateInstance)
2. Call "Release" after unuse interface.
3. Call "AddRef" after copy a interface point


HRESULT hr = pIUnknown->QueryInterface(IID_IX, (void **)&pIX)
if (SUCCEEDED(hr))
{
pIX->Fx();
pIX->Release(); //Release if succeeded
}
pIUnknown->Release();


equal:

HRESULT hr = pIUnknown->QueryInterface(IID_IX, (void **)&pIX)
pIUnknown->Release();
if (SUCCEEDED(hr))
{
pIX->Fx();
pIX->Release(); //Release if succeeded
}

for 3:

IX *pIX2 = pIX; //Make a copy of pIX
pIX2->AddRef();
...
pIX2->Release();
pIX->Release();

没有评论: