471,306 Members | 1,226 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,306 software developers and data experts.

Object referenced by events can be garbage collected !

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 ?
Jan 9 '06 #1
9 2006
Olivier Fermy wrote:
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.

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Jan 9 '06 #2
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".
Jan 9 '06 #3
Lasse Vgsther Karlsen wrote:
Olivier Fermy wrote:
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.


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

Jan 9 '06 #4
Olivier Fermy wrote:
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

Jan 9 '06 #5
Jon Skeet [C# MVP] wrote:
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

Jan 9 '06 #6
Jon Skeet [C# MVP] wrote:
Lasse Vgsther Karlsen wrote:
Olivier Fermy wrote:
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.

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.

--
Lasse Vgsther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Jan 9 '06 #7
Jon Skeet [C# MVP] wrote:
Jon Skeet [C# MVP] wrote:
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


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

--
Lasse Vgsther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Jan 9 '06 #8
Lasse Vgsther Karlsen wrote:
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 :)

<snip>
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

Jan 9 '06 #9
Thank you. Indeed i've got it the wrong way around !
Jan 9 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Scott Robinson | last post: by
1 post views Thread by Kent Johnson | last post: by
6 posts views Thread by Howard Weiss | last post: by
13 posts views Thread by Wilfried Mestdagh | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.