Garbage collection behavior for object passed out to COM event

  • Thread starter news.microsoft.com
  • Start date
N

news.microsoft.com

We are using concepts described in:
http://www.atalasoft.com/cs/blogs/r...fe-for-scripting-and-hooking-into-events.aspx

(I actually originally found it on a Microsoft website, can't find it at the
moment).

Question, say we have an event defined on our C# object, on which we call
the handlers asynchronously.

public class ComObj
{
private void SomeMethod()
{
A a = new A();
InvokeEvent(MyEvent, a);
}
}

Where InvokeEvent is a helper method that asynchronously invokes any
delegates registered to the event "MyEvent".

In this case, there is an HTML page hosting this ActiveX control. A handler
for "MyEvent" is in place, and correctly gets called.

My question is, what is the garbage collection behavior with regards to the
instance of "A"? Does the Garbage Collector KNOW that the Javascript has a
reference to it? Or, is it going to see the object go out of scope and
potentially clean it up all of a sudden?

Thanks,
Adam
 
J

Jesse Houwing

Hello news.microsoft.com,
We are using concepts described in:
http://www.atalasoft.com/cs/blogs/rickm/archive/2009/06/03/net-2-0-act
ivex-control-gotchas-safe-for-scripting-and-hooking-into-events.aspx
(I actually originally found it on a Microsoft website, can't find it
at the moment).

Question, say we have an event defined on our C# object, on which we
call the handlers asynchronously.

public class ComObj
{
private void SomeMethod()
{
A a = new A();
InvokeEvent(MyEvent, a);
}
}

Where InvokeEvent is a helper method that asynchronously invokes any
delegates registered to the event "MyEvent".

In this case, there is an HTML page hosting this ActiveX control. A
handler for "MyEvent" is in place, and correctly gets called.

My question is, what is the garbage collection behavior with regards
to the instance of "A"? Does the Garbage Collector KNOW that the
Javascript has a reference to it? Or, is it going to see the object
go out of scope and potentially clean it up all of a sudden?

If it isn't inside a .NET Scope, (which it won't be after leaving he method),
it will be collected at some point. There are wayst to instruct the GC to
keep this object alive until you think it is no longer needed (using gc.KeepAlive,
see http://msdn.microsoft.com/en-us/library/system.gc.keepalive.aspx). keep
in mind that this effectively makes you responsible to notify the Garbage
Collector when you no longer need this object, otherwise it will not be removed
from memory, until the AppDomain is unloaded.
 

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