COM Reference Counting Rules for Developers
rustWhen working with COM objects, follow these three fundamental rules for reference management:
- In Parameters (Caller to Callee): The caller maintains the reference for the duration of the method call. The callee (you) should not call
AddReforReleaseon these objects during the synchronous call. - Out Parameters (Callee to Caller): The callee provides the object with a reference already taken. The caller owns the reference and is responsible for calling
Releasewhen finished. - Copying Pointers: If you need to store a COM object pointer for use outside the immediate scope of a method call (e.g., in an async handler), you must manually call
AddRefto create a copy andReleaseonce the asynchronous work is complete.