473,465 Members | 1,444 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1354
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: WebRod | last post by:
Bonjour, comment savoir en moins de 5 secondes (enfin rapidement quoi) combien de nouveaux messages non récupérés se trouvent dans une BAL sur une serveur POP3 (pas d'imap donc)???? (en sachant...
0
by: Laurent Pointal | last post by:
Bienvenue sur la liste Python francophone, hébergée par l'AFUL, ou sur le newsgroup fr.comp.lang.python ("fclp"). Votre abonnement à cette liste de diffusion ou votre lecture de fclp montrent...
0
by: Michael Dyson | last post by:
M. Michael DYSON DIRECTEUR-ADJOINT SOCIÉTÉ DE SÉCURITÉ SARL. TEL.00229 20 21 80 Cotonou République du Bénin Email:michaeldyson2005@latinmail.com Bonjour . Je sais que mon message sera d’une...
0
by: Alejandro Scomparin | last post by:
FORMULACION, PREPARACION Y EVALUACION ECONOMICA Y FINANCIERA DE PROYECTOS DE TECNOLOGIA INFORMATICA (IT) en la UTN Inicia: 17 de Mayo. 19 hs Dirigido a Esta preparado para gerentes, jefes,...
0
by: Alejandro Scomparin | last post by:
FORMULACION, PREPARACION Y EVALUACION ECONOMICA Y FINANCIERA DE PROYECTOS DE TECNOLOGIA INFORMATICA (IT) en la UTN Inicia: 17 de Mayo. 19 hs Dirigido a Esta preparado para gerentes, jefes,...
4
by: Albert | last post by:
Bye, all: I'm unsubscribing from thiis Usenet group! You guys only talk SHIT Albert
0
by: gandalf | last post by:
Con motivo del enorme esfuerzo realizado para visitar la ciudad argentina de Córdoba, participar en la Reunión del MERCOSUR, en la clausura de la Cumbre de los Pueblos en la histórica Universidad...
1
by: crow | last post by:
http://www.pagina12.com.ar/diario/elpais/1-72984-2006-09-14.html Por Miguel Bonasso Desde La Habana Me había preparado para verlo, pero la realidad fue mucho más fuerte. Incluso le llevaba de...
1
by: gandalf | last post by:
CON LA PASION DE SIEMPRE HABLO DE CHAVEZ, DE LA MEDICINA CUBANA... Y DE SU PROPIA MUERTE Relato de la nueva gran batalla de Fidel El líder cubano mostró cómo evoluciona su recuperación en el...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.