If you are returning
an interface pointer, you don't want to release it, because it will be invalid by the time the caller gets it. You need that copy of the pointer to be counted, otherwise when the caller releases it, the count will be incorrect. So if you're copying the pointer from somewhere else (such as the wrapper object, via ComObjValue) and then returning it, you need to AddRef.
But you aren't returning the interface pointer.
Just think about how the reference count will change.
- Say obj contains a wrapper object with the only copy of the interface pointer, so refcount = 1.
- ComObjValue returns the interface pointer, still with refcount = 1.
- ObjAddRef is called, refcount = 2.
- You do some stuff which doesn't affect the refcount (as far as you know).
- ObjRelease is called, refcount = 1.
If the function is called again, the result will be the same. If ObjRelease wasn't called, the reference count would increase by 1 each time you call the function.
In this case, I wouldn't bother with AddRef or Release. You know that from start to finish, obj
will contain a reference to the wrapper object. There is no documented way to modify the value (interface pointer) of the wrapper object, so it will remain valid until the wrapper object is freed, which won't happen until after the function returns.