event handlers and GC

C

cs

I have an object X, and an object Y.
Y is the only object that has a reference to X.
X has an event. Objects A, B, C register for this event but dont keep a
reference to object X.
When whoever owns Y is done with it, and Y is garbag collected, what happens
to X and to the Objects that registered for the event? will those event
registration prevent X from being garbage collected?
 
N

Nicholas Paldino [.NET/C# MVP]

cs,

When A, B, and C register for the event on X, X has a reference to those
objects (assuming they are instances). If Y releases X, and nothing else
has references to A, B, or C, then X, A, B, and C all become eligible for
GC.

Hope this helps.
 
A

Anders Borum

Yes

The problem is, however, that if A, B and C goes out of scope, they're still
registered for the event in X, and will cause major problems when not
accessible. Looking forward to some kind of weak reference in .NET where
objects can be unregistered when CG'ed.
 
C

cs

Nicholas Paldino said:
cs,

When A, B, and C register for the event on X, X has a reference to those
objects (assuming they are instances). If Y releases X, and nothing else
has references to A, B, or C, then X, A, B, and C all become eligible for
GC.

Hope this helps.


nice thats exactly how I was hoping it would behave. In my case A,B,C are
only created to handdle the event, and no one keeps a reference to them,
other than the event they registered for.
 

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