Object referenced by events can be garbage collected !

G

Guest

I have created a sample project where i have referenced an object only with
an event :
textBox.VisibleChanged += new EventHandler(this.textBox_VisibleChanged);

When i call GC.Collect(), the object is disposed and garbage collected ! I
thought that event references are strong references and not weak references.
Does the .NET 2.0 garbage collector manage differently the events references ?
 
G

Guest

Olivier said:
I have created a sample project where i have referenced an object only with
an event :
textBox.VisibleChanged += new EventHandler(this.textBox_VisibleChanged);

When i call GC.Collect(), the object is disposed and garbage collected ! I
thought that event references are strong references and not weak references.
Does the .NET 2.0 garbage collector manage differently the events references ?

Which object is collected? "this" or the textbox?

If you mean this, then there's nothing in your code here that would make
sure "this" is kept in memory. If there's no other reference to this,
then it will die out.

Now, forms might be different though, I'm not entirely sure a form will
die on its own if it's visible.

Can you give us more details about what type of object that is
collected, how you use it, wether you have any extra references to it, etc.
 
G

Guest

The "textBox" is collected when i call GC.Collect, even though the "Form"
referencing "textBox.VisibleChanged" event is still alive and visible.

It seems that the "VisibleChanged" event delegate reference between the
"Form" and the "TextBox" is considered as a weak reference.

Nota : There is no other references between the "Form" and the "TextBox",
the "TextBox" is created as a local variable in the "Form" constructor only
for testing. This is not a control owned or displayed by the "Form".
 
J

Jon Skeet [C# MVP]

Lasse said:
Which object is collected? "this" or the textbox?

If you mean this, then there's nothing in your code here that would make
sure "this" is kept in memory. If there's no other reference to this,
then it will die out.

Yes there is. There's the delegate. The delegate has a target object,
which in this case is "this". That certainly *should* prevent the
garbage collector from collecting it.

Jon
 
J

Jon Skeet [C# MVP]

Olivier said:
The "textBox" is collected when i call GC.Collect, even though the "Form"
referencing "textBox.VisibleChanged" event is still alive and visible.

Yes - the reference doesn't go that way.
It seems that the "VisibleChanged" event delegate reference between the
"Form" and the "TextBox" is considered as a weak reference.

No, you've got it the wrong way round. An event has a number of
delegates, and each of those delegates potentially has a reference to a
target object. If I do:

x.SomeEvent += new EventHandler (y.SomeHandler);

then as long as x is alive, y will be kept alive too (leaving aside the
possibilities of unsubscribing etc).

However, nothing in the above is preventing x from being garbage
collected.

Jon
 
J

Jon Skeet [C# MVP]

Jon said:
Yes there is. There's the delegate. The delegate has a target object,
which in this case is "this". That certainly *should* prevent the
garbage collector from collecting it.

Sorry - to be clearer, it would prevent the garbage collector from
collecting "this" while there was a strong reference to the textbox. If
the textbox is eligible for garbage collection, then its event handlers
have no impact on what else is eligible/ineligible for garbage
collection.

Jon
 
?

=?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=

Jon said:
Yes there is. There's the delegate. The delegate has a target object,
which in this case is "this". That certainly *should* prevent the
garbage collector from collecting it.

Jon

I have had too little coffee today. I didn't mean "this", I meant the
textbox in the text above: 'If you mean the textbox, then there's
nothing in your ...'.

Sorry about that.

Since he said that it's the textbox that is collected then this delegate
will not keep the textbox alive. If it should then I'm way off on my
memory management knowledge of .NET.

Only thing I'm not 100% sure of is if I construct a form object, show
it, and forget about it, then I assume it will continue living, which
means it itself stored a reference somewhere, I assume somewhere in the
runtime where a list of "active forms" is kept. I'd have to test to be
sure but so far things seems to work like I expect them to so I expect
the form to live until closed.

However, the textbox is no better off if he has no reference to it, and
I don't see how the delegate would matter for the textbox.
 
?

=?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=

Jon said:
Sorry - to be clearer, it would prevent the garbage collector from
collecting "this" while there was a strong reference to the textbox. If
the textbox is eligible for garbage collection, then its event handlers
have no impact on what else is eligible/ineligible for garbage
collection.

Jon

We're on the same page then and I'll get more coffee in the system
before future posts :)
 
J

Jon Skeet [C# MVP]

Lasse said:
I have had too little coffee today. I didn't mean "this", I meant the
textbox in the text above: 'If you mean the textbox, then there's
nothing in your ...'.

Ah :)

Only thing I'm not 100% sure of is if I construct a form object, show
it, and forget about it, then I assume it will continue living, which
means it itself stored a reference somewhere, I assume somewhere in the
runtime where a list of "active forms" is kept. I'd have to test to be
sure but so far things seems to work like I expect them to so I expect
the form to live until closed.

I believe it will continue living until it is closed. In particular,
Application.Run calls Dispose on the form passed to it when it returns.
However, the textbox is no better off if he has no reference to it, and
I don't see how the delegate would matter for the textbox.

Absolutely.

Jon
 

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