Groups | Blog | Home
all groups > dotnet interop > march 2008 >

dotnet interop : Passing a RCW to another RCW method


Nishith Prabhakar
3/29/2008 7:42:06 AM
Hi,

I am facing some memory leak problems with COM interop cleanup. Will
the following code cause any memory leaks because of the
wrapper.methodCall method (part of the exposed COM interface).

1) Is the refcount incremented when the methodCall is made?
2) If yes, where is the refcount - in "wrapper" or in "otherWrapper"
3) How should the reference created by the methodCall( ) be
decremented?

Regards
Nishith

MyFirstComRCWWrapper wrapper = new MyFirstComRCWWrapper();

MyOtherRCWWrapper otherWrapper = new MyOtherRCWWrapper();

int result = wrapper.methodCall(otherWrapper); // Will this cause a
memory leak?

Marshal.FinalReleaseComObject(otherWrapper);
otherWrapper = null;

// Release on other wrapper
Marshal.FinalReleaseComObject(wrapper);
Adam
3/29/2008 10:28:00 PM
This code shouldn't be causing any leaks in itself.

In general, the RCW does AddRef during its construction, and the Release is
called during finalization. No AddRef is performed by .NET when you call
methodCall. You shouldn't even need to call Marshal.FinalReleaseComObject
because when the garbage collector collects your RCWs the underlying COM
references will be released.

Now, if your implementation wrapper.methodCall performs AddRef, then that
may be where you are leaking. Circular references in COM are known to be an
issue. Are the COM classes yours or are they third party?

Adam


[quoted text, click to view]
Christian Fröschlin
3/31/2008 11:37:25 AM
[quoted text, click to view]

However, explicit releasing may be preferrable if the COM object
uses a lot of native resources which the GC knows nothing about,
AddThis Social Bookmark Button