Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / March 2008

Tip: Looking for answers? Try searching our database.

Passing a RCW to another RCW method

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nishith Prabhakar - 29 Mar 2008 15:42 GMT
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);
wrapper = null;
Adam - 30 Mar 2008 06:28 GMT
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

> Hi,
>
[quoted text clipped - 23 lines]
> Marshal.FinalReleaseComObject(wrapper);
> wrapper = null;
Christian Fröschlin - 31 Mar 2008 10:37 GMT
> 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.

However, explicit releasing may be preferrable if the COM object
uses a lot of native resources which the GC knows nothing about,
otherwise, the GC may not start collecting early enough.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.