I am writing a .NET application where I have to use API calls (really). My application gets COM interfaces by calling Win API's function CoCreateInstance()
.
Of course, I have to release those interfaces when having finished with them. I think that the appropriate method to do so is Marshal.Release()
(at least, no other reasonable method comes to my mind).
Now, I would like to make every respective interface pointer a safe handle. To do that, I have to derive my own safe handle class from SafeHandle
and have to override ReleaseHandle()
in the derived class. The respective documentation states:
[...] In particular, apply the ReliabilityContractAttribute attribute to any methods you call from ReleaseHandle. In most cases this code should be: ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success) [...]
This is my problem: I do not know if Marshal.ReleaseHandle()
already has this contract by default, and if not, how I could add it. I think I am lacking some basics here. Could somebody out there please shed some light on it?
The definition of the abstract method Marshal.Release
is
[System.Security.SecurityCritical] // auto-generated_required
[ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
public static extern int /* ULONG */ Release(IntPtr /* IUnknown */ pUnk );
So it is already decorated with the ReliabilityContractAttribute. You can check the source of any framework type by using a decompiler (I used JustDecompile) or over at referencesource.microsoft.com.