By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,088 Members | 1,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,088 IT Pros & Developers. It's quick & easy.

COM Releasing .NET Object

P: n/a
I have an application written in C++ (NOT .NET) which creates an instance of
a vb .net object.

My application releases the .net object when it has finished using it.
However .NET GC is not destroying the object until my application closes
which can be minutes or hours later.

How can my C++ application force the .NET object to be TOTALY destroyed when
I release it.

Thanks.

--
Michael Tissington
http://www.oaklodge.com
http://www.tabtag.com

Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
TDC
Is the VB.NET object one you wrote? If it is, then implement a Dispose
method that performs all cleanup explicitly. That way you don't have
to worry about the garbage collector.

Tom

Nov 21 '05 #2

P: n/a
"Michael Tissington" <mi*****@nospam.com> wrote in message
news:eL****************@tk2msftngp13.phx.gbl...
How can my C++ application force the .NET object to be TOTALY destroyed
when I release it.


According to .NET and COM guru Adam Nathan, you should call Dispose from the
COM client in the same way as you would in a .NET app.

If you can't modify the COM client, you can implement a custom marshaler
that calls Dispose when the CCW's ref count reaches zero, but it is complex.

See a post by David Stucki in microsoft.public.dotnet.framework.interop.

Tech blog
http://www.itwriting.com/blog/
Nov 21 '05 #3

P: n/a
I'm writing both the .NET object and the COM client.

So how does the COM client call Dispose on the .NET Object ?

Does the .NET object need to impalement Dispose ?

The problem seems to be related to the COM client using IConnectionPoint to
ask for Events.
Then when the client frees the Connection the .NET object does not release
the client until the .NET object has been really destroyed (at the moment I
have resorted to GC.Collect() to force this).

--
Michael Tissington
http://www.oaklodge.com
http://www.tabtag.com
"Tim Anderson" <ti*****@hotmail.com> wrote in message
news:us**************@TK2MSFTNGP09.phx.gbl...
"Michael Tissington" <mi*****@nospam.com> wrote in message
news:eL****************@tk2msftngp13.phx.gbl...
How can my C++ application force the .NET object to be TOTALY destroyed
when I release it.


According to .NET and COM guru Adam Nathan, you should call Dispose from
the COM client in the same way as you would in a .NET app.

If you can't modify the COM client, you can implement a custom marshaler
that calls Dispose when the CCW's ref count reaches zero, but it is
complex.

See a post by David Stucki in microsoft.public.dotnet.framework.interop.

Tech blog
http://www.itwriting.com/blog/

Nov 21 '05 #4

P: n/a

"Michael Tissington" <mi*****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
I'm writing both the .NET object and the COM client.

So how does the COM client call Dispose on the .NET Object ?
Just implement iDisposable in the .NET object. Write code for the Dispose
method that cleans up resources. Then have the COM client call Dispose.

Note this won't actually destroy the object, but it means you can release
resources.
Does the .NET object need to impalement Dispose ?
Yes.
(at the moment I have resorted to GC.Collect() to force this).


Purists will say not to use GC.Collect() but in a situation like this I
wouldn't hesitate if it solves the problem.

Tim
Tech blog
http://www.itwriting.com/blog/
Nov 21 '05 #5

P: n/a
Tim,
| Purists will say not to use GC.Collect() but in a situation like this I
| wouldn't hesitate if it solves the problem.
I would question if it actually solves the problem or simply *masks* the
problem.

By masking the problem I mean making it appear the problem is solved.

I hope you agree that masking the problem is not really the same as solving
the problem. Although masking the problem sometimes is the only "viable"
solution.

For details on when to call GC.Collect see Rule #1 at:
http://blogs.msdn.com/ricom/archive/...29/271829.aspx
http://blogs.msdn.com/ricom/archive/.../02/40780.aspx


Just remember that "the collector is self-tuning so don't mess with it"!

Hope this helps
Jay
"Tim Anderson" <ti*****@hotmail.com> wrote in message
news:eo*************@TK2MSFTNGP11.phx.gbl...
|
| "Michael Tissington" <mi*****@nospam.com> wrote in message
| news:%2****************@TK2MSFTNGP14.phx.gbl...
| > I'm writing both the .NET object and the COM client.
| >
| > So how does the COM client call Dispose on the .NET Object ?
|
| Just implement iDisposable in the .NET object. Write code for the Dispose
| method that cleans up resources. Then have the COM client call Dispose.
|
| Note this won't actually destroy the object, but it means you can release
| resources.
|
| > Does the .NET object need to impalement Dispose ?
|
| Yes.
|
| > (at the moment I have resorted to GC.Collect() to force this).
|
| Purists will say not to use GC.Collect() but in a situation like this I
| wouldn't hesitate if it solves the problem.
|
| Tim
| Tech blog
| http://www.itwriting.com/blog/
|
|
Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.