471,354 Members | 2,121 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Unsubscribing from de-referenced object's event?

Do I need to unsubscribe from an object's event before I point the object
variable to a new object?

I have a method that subscribes an event handler to the ListChanged event of
a list. The list is attached to a calendar, so every time a new date is
selected, the list is recreated for the new date. The code then subscribes
the event handler to the new list. Here's the cose (m_ListForSelectedDay is
a member variable):

private void calendar_DateChanged(object sender, DateRangeEventArgs e)
{
// Initialize
DateTime newDate = e.Start;

// Get daily ist for new date
m_ListForSelectedDay = GetDailyList(newDate);

// Subscribe to ListChanged event
m_ListForSelectedDay.ListChanged += new
ListChangedEventHandler(ListForSelectedDay_ListCha nged);
}

Do I need to unsubscribe from the ListChanged event before I point the
ListForSelectedDay variable to a new list? If I de-reference the object, but
I still have an event handler subscribed to it, will that keep the object
from being garbage-collected? Thanks in advance.

--
David Veeneman
Foresight Systems
Jan 16 '06 #1
4 1281
"David Veeneman" <da****@nospam.com> wrote in
news:e4**************@TK2MSFTNGP11.phx.gbl:
Do I need to unsubscribe from an object's event before I point the
object variable to a new object?


Most definitely. I don't think it will prevent the object from going out
of scope and being GC'ed, but until that happens you might still receive
events from it.

Let me put it this way: its easy to do, and its the right thing to do. So
do it.

-mdb
Jan 16 '06 #2
David Veeneman <da****@nospam.com> wrote:
Do I need to unsubscribe from an object's event before I point the object
variable to a new object?


Well, there are two issues here:

1) Getting an event from the wrong object. If you don't unsubscribe
from the event but the object raises the event, your handler will still
be called.

2) Garbage collection. The "owner" of the event keeps a reference to
the delegates in the event, which (for non-static methods) includes
keeping a reference to the "target" of the delegate. However, there's
no reference the other way. In other words, the owner of the event can
prevent the subscriber from being garbage collected, but not the other
way round.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 16 '06 #3
Me
I agree with ya on that.

A button keeps a reference to the form it is on, but a Form does not keep a
reference to the button.. Well.. it does.. but not from an event stand point
:-)
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
David Veeneman <da****@nospam.com> wrote:
Do I need to unsubscribe from an object's event before I point the object
variable to a new object?


Well, there are two issues here:
2) Garbage collection. The "owner" of the event keeps a reference to
the delegates in the event, which (for non-static methods) includes
keeping a reference to the "target" of the delegate. However, there's
no reference the other way. In other words, the owner of the event can
prevent the subscriber from being garbage collected, but not the other
way round.

Jan 17 '06 #4
Me <me@home.com> wrote:
I agree with ya on that.

A button keeps a reference to the form it is on, but a Form does not keep a
reference to the button.. Well.. it does.. but not from an event stand point
:-)


Well, the button's events keep a reference to whatever the target of
their handlers are - that needn't be a form :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 17 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by WebRod | last post: by
reply views Thread by Laurent Pointal | last post: by
reply views Thread by Michael Dyson | last post: by
reply views Thread by Alejandro Scomparin | last post: by
1 post views Thread by gandalf | 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.