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 / Component Services / July 2003

Tip: Looking for answers? Try searching our database.

.NET: garbage collection for data type returrned from COM Interop

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JC - 08 Jul 2003 21:11 GMT
How does garbage collection work in C# and VB.NET for data returned from COM
object?  For example, a COM object written in C++ returns a SAFEARRAY to C#
or VB.NET, will this SAFEARRAY (mapped to type[] in C# or VB.NET) be garbage
collected?  Similarily, if C# or VB.NET pass SAFEARRAY to the COM object via
COM method invocation, will this SAFEARRAY be garbage collected after the
call?

My guess to answers for both questions is yes.  Thanks.
Nicholas Paldino [.NET/C# MVP] - 08 Jul 2003 21:33 GMT
JC,

   When you pass a SAFEARRAY from unmanaged code to managed code, what
happens as far as garbage collection depends on the types that are in the
safearray.

   First off, the safearray is always released.

   Before it is released, the runtime is going to marshal the elements from
the safearray from the unmanaged realm over to the managed realm.  If the
safearray contains primitive types, then those are marshaled across into the
new managed array.  If the safearray contains interface pointers, then those
are marshaled across, and the reference count is incremented by one on each
of them, assuring that they are alive when accessed by managed code.  Then,
when the SAFEARRAY is deallocated (when the marshaling is complete), the
reference count will be reduced by one (which is fine, since the initial
entry of the item into the safe array should have incremented the reference
count by one), resulting in a net change of plus one for the reference
count, that reference being held by the managed wrapper for the COM object.

   Now, these managed representations are subject to the same rules as any
other managed object, and will be garbage collected if they can not be
reached.  When GCed, the reference count will be decremented by one, unless
a call to ReleaseComObject is made on the Marshal class, passing the COM
object.  This will reduce the reference count by one at that point, which is
good practice, releasing it when you need it.

   As for going the other way, when marshaling a .NET object into the
unmanaged realm, the runtime keeps a reference to it.  When the COM-callable
wrapper is released (the reference count is set to zero), then the runtime
releases the reference, and it is subject to garbage collection, assuming
there are no other references.

   Hope this helps.

Signature

              - Nicholas Paldino [.NET/C# MVP]
              - nicholas.paldino@exisconsulting.com

> How does garbage collection work in C# and VB.NET for data returned from COM
> object?  For example, a COM object written in C++ returns a SAFEARRAY to C#
[quoted text clipped - 4 lines]
>
> My guess to answers for both questions is yes.  Thanks.

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.