Thanks Elysia,
I do not agree with "So you shouldn't call Release() before reassigning". I think you mean write code like this,
Code:
pI = pI2;
pI -> Release();
pI -> AddRef();
other than like
Code:
pI -> Release(); // release before binding to a new interface
pI = pI2;
pI -> AddRef();
Ths problem of your comment is pI1 may pointed to another component, and we need to call "its (the component pointed by pI1)" Release, other than pI2's Release.
Please feel free to correct me if I am wrong.
Originally Posted by
Elysia
AddRef() adds one to the reference count and Release() decrements it by one.
When the reference count reaches 0, the object is destroyed. So you shouldn't call Release() before reassigning. Call it after.
Even better it to wrap the COM object in a RAII class where the constructor calls AddRef and the destructor calls Release().
regards,
George