Event regsitration and finalizer

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

When an object is register to event (delegate marked as event) with one of
its methods, what happen when the event is raised if the object is dead
(finalized)?
In this case the delegate invocation is trying to invoke a method on a null
object, this should generate en exception.
What exception is that?


Another question in this same issue:

If an object A is registered to an event on some other object B, and object
A is out of context, it should be finalized. BUT I see that because one of
its method is still registered to the event on object B, it's like object B
is still referencing object A. therefore object A is not being finalized even
if I expect it to.
Am I correct?
 
When an object is register to event (delegate marked as event) with
one of
its methods, what happen when the event is raised if the object is
dead
(finalized)?
In this case the delegate invocation is trying to invoke a method on a
null
object, this should generate en exception.
What exception is that?
Another question in this same issue:

If an object A is registered to an event on some other object B, and
object
A is out of context, it should be finalized. BUT I see that because
one of
its method is still registered to the event on object B, it's like
object B
is still referencing object A. therefore object A is not being
finalized even
if I expect it to.
Am I correct?

I'm pretty certain that the object will not be finalized by the garbage collector
because the event's delegate still maintains a reference to it. So, the object
will be kept alive until it removes itself from the event.

Best Regards,
Dustin Campbell
Developer Express Inc.
 
Thank you all for confirming my guess.

But still you haven't answer my first question:

When an object is register to event (delegate marked as event) with one of
its methods, what happen when the event is raised if the object is dead
(finalized)???
In this case the delegate invocation is trying to invoke a method on a null
object, this should generate en exception.
What exception is that???
 
But still you haven't answer my first question:

I think they did.

When an object is register to event (delegate marked as event) with one of
its methods, what happen when the event is raised if the object is dead
(finalized)???

That should generally not happen, since the delegate keeps the object
alive.

The only situation I can imagine that this could happen is if both
objects (the event provider and subscriber) are being finalized, the
subscriber is getting finalized first and then the provider raises the
event from within its finalizer. In that case the class is extremely
poorly written.

In this case the delegate invocation is trying to invoke a method on a null
object, this should generate en exception.

There's no such thing as a "null object". The object may have cleaned
up all its native resources and thus be in an invalid state, but that
doesn't automatically raise any exception.


Mattias
 
Mattias Sjögren said:
I think they did.



That should generally not happen, since the delegate keeps the object
alive.

The only situation I can imagine that this could happen is if both
objects (the event provider and subscriber) are being finalized, the
subscriber is getting finalized first and then the provider raises the
event from within its finalizer. In that case the class is extremely
poorly written.

The garbage collector handles this too, it's called resurrection and it will
cause the event subscriber to survive until the next collection, and
sometime even be promoted into the next generation. Although it is still
possible for the call to occur after the finalizer has run, resurrection
guarantees that the object has not been reclaimer by the GC. This is why
any object with unmanaged resources should be disposed -- because the
finalizer may not be run when expected, may not run at all, or may run
twice.
 

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

Back
Top