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

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -