Releasing CComPtr
-
Well, I have a question about the usuage of CComPtr, but I think I know the answer, but would like to make sure I understand this completely... CComPtr is a smart pointer class for managing COM interface pointers, thus I don't need to worry about calling AddRef for Release methods, since they are automatically called. From what I've read, the Release method will be called when the CComPtr goes out of scope. I'm have the task of maintaining a COM client app (first time with COM) and I was thinking about 'reusing' a particular instance of a CComPtr (no need to declare a new one, since there's one already!) The only thing that had me worried what that by reusing the CComPtr does that screw up the reference counting and create a memory leak... psuedocode below: { CComPtr myIsomething = (some Isomething interface) ...do stuff to the CComPtr... ...lets reuse it... myIsomething = (some other Isomething interface) } Since the myIsomething didn't go out of scope when I set "myIsomething" to another interface, I was worried that it may be an issue. So after a day of digging around this is my conclusion: This is actually OK, since if we look at the definition of this template, the = operator is overloaded as such: T* operator=(T* lp) { return (T*)AtlComPtrAssign((IUnknown**)&p, lp); } When I looked up AtlComPtrAssign, it seems that it will call release on the interface pointer, prior to reassignment to the new interface. If that's true, there shouldn't be any issues with reference counting and I can continue on my merry way?
-
Well, I have a question about the usuage of CComPtr, but I think I know the answer, but would like to make sure I understand this completely... CComPtr is a smart pointer class for managing COM interface pointers, thus I don't need to worry about calling AddRef for Release methods, since they are automatically called. From what I've read, the Release method will be called when the CComPtr goes out of scope. I'm have the task of maintaining a COM client app (first time with COM) and I was thinking about 'reusing' a particular instance of a CComPtr (no need to declare a new one, since there's one already!) The only thing that had me worried what that by reusing the CComPtr does that screw up the reference counting and create a memory leak... psuedocode below: { CComPtr myIsomething = (some Isomething interface) ...do stuff to the CComPtr... ...lets reuse it... myIsomething = (some other Isomething interface) } Since the myIsomething didn't go out of scope when I set "myIsomething" to another interface, I was worried that it may be an issue. So after a day of digging around this is my conclusion: This is actually OK, since if we look at the definition of this template, the = operator is overloaded as such: T* operator=(T* lp) { return (T*)AtlComPtrAssign((IUnknown**)&p, lp); } When I looked up AtlComPtrAssign, it seems that it will call release on the interface pointer, prior to reassignment to the new interface. If that's true, there shouldn't be any issues with reference counting and I can continue on my merry way?
Hello alchong, >>...lets reuse it... >>myIsomething = (some other Isomething interface) Yes, this is OK AFAIK. One reusage problem that I have encountered before is related to QueryInterface(). If you were to do this : ISomething -> QueryInterface(IID_ISomething, (void**)&myIsomething); you will get an assertion failure if myIsomething is already holding onto another interface pointer. You can overcome reusage problems by first assigning myIsomething to NULL as in : myIsomething = NULL; before reusing it. - Bio.
-
Hello alchong, >>...lets reuse it... >>myIsomething = (some other Isomething interface) Yes, this is OK AFAIK. One reusage problem that I have encountered before is related to QueryInterface(). If you were to do this : ISomething -> QueryInterface(IID_ISomething, (void**)&myIsomething); you will get an assertion failure if myIsomething is already holding onto another interface pointer. You can overcome reusage problems by first assigning myIsomething to NULL as in : myIsomething = NULL; before reusing it. - Bio.