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 / November 2003

Tip: Looking for answers? Try searching our database.

COM object lifetime in managed code

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Assaf - 27 Nov 2003 16:17 GMT
hey all.

i am allocating an object using
Activator.CreateInstance(System.Type);
I get a reference to the Com object.
however, i am not sure how to close down the object that i have.

do i need to call Marshal.ReleaseComObject()?
will the garbage collector collect it without me calling it
ReleaseComObject?

what about all the interfaces that i 'cast' out of the Com object?
do i need to call ReleaseComObject for each one of them?

what about calling ReleaseComObject in general,
do i need to call it in order to release the object immediately,
meaning, if i do not call ReleaseComObject, than the garbage collector will
do it for me?

what about Com interfaces that are pointing to each other,
in an intricate mesh?
like a linked list?
do i need to release the list each node in an orderly manner?
or simply stop pointing at its head?
meaning, if i stop pointing at the head, will the entire linked list be
released?

what about hardware resources held by a Com interface,
do i need to tell the Com interface to release the resource?
or can i simply stop pointing at the Com interface and expect the garbage
collector to release all hardware resources?

please, i know that i am asking a lot,
but i do not know where to read about this.

if anyone has a source of information that i can read this,
i would be greatfull if you point me at it.

thank a lot.

assaf
Mattias Sj?gren - 27 Nov 2003 17:51 GMT
>do i need to call Marshal.ReleaseComObject()?

If you want to know when the object is realeased,  yes.

>will the garbage collector collect it without me calling it
>ReleaseComObject?

Yes, but you don't know exactly when.

>what about all the interfaces that i 'cast' out of the Com object?
>do i need to call ReleaseComObject for each one of them?

No, you call ReleaseComObject once per object (or actually once for
each time the object has been marshaled into managed code).

>what about calling ReleaseComObject in general,
>do i need to call it in order to release the object immediately,
>meaning, if i do not call ReleaseComObject, than the garbage collector will
>do it for me?

Exactly

>meaning, if i stop pointing at the head, will the entire linked list be
>released?

Any objects that you no longer reference will be collected at the next
GC.

>what about hardware resources held by a Com interface,
>do i need to tell the Com interface to release the resource?

The COM object should release any resorces it still holds at the last
Release call, before the object is destroyed.

>but i do not know where to read about this.

http://blogs.gotdotnet.com/cbrumme/permalink.aspx/940d1191-3cc5-4b8d-a8b3-68b47f
2f8dcb


(The link currently doesn't work here).

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.


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.