Dispose of com objects without waiting for gc

G

gerry

I have a com object that is used via an interop assembly - ie. I referenced
the com object and vs.net created the interop .dll for it.
I create one object per session when the page 1st loads and store it in the
Session. I expected that the object would be released when the Session
object was destroyed during at Session_End(). In my log I can see the
Session_End() event firing as it should after the specified idle time,
however the actual com object does not get destroyed for up to 12 hours
later. I assunme that this is due to the underlying com object only being
destroyed when the .net interop object is GC'd.

I must be missing something simple because I don't see anyway to release
these objects other than to wait for the GC to do its job. I would have
expected that the .net generated com interop object would have exposed a
Dispose method to allow for disposal of the underlying com object without
having to wait for gc.

Gerry
 
G

gerry

sure , but doesn't that defeat the whole purpose of gc if am am calling
Collect everytime a session ends ?
 
S

Steve C. Orr [MVP, MCSD]

I'm just saying that if the garbage isn't being collected when you want it
to be, then maybe you should do it manually. That's why the GC.Collect
method exists.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
 
G

gerry

well that's just it , i don't necessarily want the garbage to be collected,
that could be an undue strain on the system. I would like .net objects to
release their resources in a timely fashion without being dependant on gc.
it seems odd that there is such an emphasis on implementing the IDisposable
interface to allow the clean up resources as soon as they are no longer
needed, but for interop wrappers there is no such facility available to.

anyway i guess that the answer is that com interop is 100% dependant on gc
to release the underlying com resources.

thanks for your comments

gerry
 
S

Scott Allen

If you want to release the COM object without waiting for the GC, use
Marshal.ReleaseComObject.

HTH,
 
G

gerry

Thanks Scott, that works perfectly.

I still think a simple implementation of IDisposable by the .net com wrapper
class would have been appropriate - but hey , what do I know.

:)
 
S

Scott Allen

There are some rough edges around COM interop. To be fair, MS had some
tough decisions to make - so it is what it is I guess :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top