c++ - Why is every successful QueryInterface() call followed by Release() call? -
why queryinterface()
call followed release()
call? example, have seen sample code msdn below:
hresult hr = s_ok; cdecoder *pobj = new cdecoder(&hr); if (succeeded(hr)) { *ppv = null; hr = pobj->queryinterface(riid, ppv); } pobj->release(); return hr;
can explain intent behind release()
call here?
it's not followed directly this, though pretty common.
com objects reference counted. when create object, pointer iunknown
. obtain other interface queryinterface
. since (usually) don't care iunknown
interface anymore, release that. when release other interface obtained, reference count 0 object can destroyed. if don't release iunknown
, however, reference count stay non-zero, object can't destroyed.
the obvious case not release iunknown
when/if need more 1 other interface. in such case, you'd iunknown
, second , third interfaces before releasing iunknown
. there @ least potentially cases might not know third (or subsequent) interfaces right after created object either, might want retain access `iunknown arbitrary length of time before releasing it.
Comments
Post a Comment