I have a C# client that consumes interfaces from a native C++ COM server dll. The DLL implements 4 interfaces. These 4 interfaces are implemented by 4 coclasses in the DLL. But only 1 coclass is exposed to the client. Interfaces 2,3,4 are returned to the client by one of the methods in interfaces 1.
C++ COM server:
interface IFace1: IUnknown{
HRESULT CreateOtherInterface([in] REFIID iidFace, [out, iid_is(iidFace)] void** ppOut);
};
coclass ClassIFace1
{
[default] interface IFace1;
};
C# Client:
ClassIFace1 Face1Obj = new ClassIFace1();
IFace1 Face1Ctrl = (IFace1)Face1Obj;
IFace2 Face2Ctrl = null;
IntPtr Face2IntPtr = new IntPtr();
Face1Ctrl.CreateOtherInterface(Face2Guid, out Face2IntPtr);
Face2Ctrl = (IFace2)Mashal.PtrToStructure(Face2IntPtr);
//Consume Face2Ctrl
if(Face1Obj != null)
{
Marshal.ReleaseComObject(Face1Obj);
}
As IFace2, IFace3 and IFace4 does not share the same coclass as IFace1, I suspect the Marshal.ReleaseComObject(Face1Obj) line will only destruct ClassIFace1 object but not ClassIFace2, ClassIFace3, ClassIFace4 objects and results in memory leaks. Is there any way to solve this? Or the Marshal.ReleaseComObject(Face1Obj) actually destroy other COM objects as well?