473,372 Members | 782 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 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 2172
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Scott Robinson | last post by:
I have been having trouble with the garbage collector and sockets. Unfortunately, google keeps telling me that the problem is the garbage collector ignoring dead (closed?) sockets instead of...
1
by: Kent Johnson | last post by:
If I create and start a thread without keeping a reference to the thread, when is the thread garbage collected? What I would like is for the thread to run to completion, then be GCed. I can't...
4
by: Lester | last post by:
I have a low level question . . . If I have an assembly with a single class with a static int property, I know the assembly will get loaded the first time the class is referenced. At that...
7
by: Bryan D. | last post by:
Our application makes extensive use of C#'s event handling mechanism to communicate between classes. A problem has cropped in that it becomes difficult to know for sure that all observers of an...
2
by: Filippo | last post by:
I was told that if you pass a "ref param", the garbage collector does keep a reference and therefore you need to dispose by yourself of the object itself. Is that true ? Filippo
6
by: Howard Weiss | last post by:
I am reading a 160 mB file in my application. I want to access the file as shorts. FileStream->Read wants to access the file as Byte so I do the following 1) Read the filesize 2) Create a Byte...
1
by: J | last post by:
I have an object that I am using in my ASP.net app. I only want one instance of this object available to all of my pages. Here is a sample public class statictes private statictest() {} //...
13
by: Wilfried Mestdagh | last post by:
Hi, I have an application using a DLL and callbacks. It generate random the error "A callback was made on a garbage collected delegate". I found some articles that the pointer to the delegate...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.