473,396 Members | 1,676 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,396 software developers and data experts.

Best Practice for removing objects from delegates/events before garbagecollection?

CB
When designing an object, is there a best practice for how to allow the
user of the object to tell the object to remove itself from any
delegates / events it has registered with?

For example.. say we have a static settings class, and it has some
OnChange event. The object I am writing will add a method to the
settings OnChange event in its constructor. Obviously, at some point,
the method needs to be removed from the event so the new object can be
garbage collected. Since IDispose is only supposed to be used for
cleaning up unmanaged memory, where should I put the unwire call? [if
this were c++, i would put it in the destructor, but that isn't an
option in C#]

It seems like the only suitable approach is the following. However, I am
curious if there is some standard naming convention for the methods.

{
// in the constructor, listener adds itself to the settings event
Listener listener = new Listener();
...
// use listener
...
listener.Detach();
listener = null;
}

Also, could move the adding of the object to the event from the objects
constructor to some Attach() method the user most call.
Thanks.
Sep 9 '06 #1
1 1722
I would put that in dispose. It is where things like cleaning up data
connections and things go. Things the garbage collector will not do
automatically.
This seems to be something exactly like that to me.

Ciaran O'Donnell

"CB" wrote:
When designing an object, is there a best practice for how to allow the
user of the object to tell the object to remove itself from any
delegates / events it has registered with?

For example.. say we have a static settings class, and it has some
OnChange event. The object I am writing will add a method to the
settings OnChange event in its constructor. Obviously, at some point,
the method needs to be removed from the event so the new object can be
garbage collected. Since IDispose is only supposed to be used for
cleaning up unmanaged memory, where should I put the unwire call? [if
this were c++, i would put it in the destructor, but that isn't an
option in C#]

It seems like the only suitable approach is the following. However, I am
curious if there is some standard naming convention for the methods.

{
// in the constructor, listener adds itself to the settings event
Listener listener = new Listener();
...
// use listener
...
listener.Detach();
listener = null;
}

Also, could move the adding of the object to the event from the objects
constructor to some Attach() method the user most call.
Thanks.
Sep 9 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Marty McDonald | last post by:
It is still unclear to me why we would use events when delegates seem to do just fine. People say that events make it so the publisher doesn't need to know about the listeners. What does that...
4
by: Chuck Ritzke | last post by:
I keep asking myself this question as I write class modules. What's the best/smartest/most efficient way to send a large object back and forth to a class module? For example, say I have a data...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
2
by: Bob Rundle | last post by:
I have the following code, which appears to be working. However it doesn't look right. The part I am wondering about is the logic in DisconnectEvents(). This logic creates a new delegate and...
1
by: MuZZy | last post by:
Hi, Is there a way to remove all event handlers for a control's event? Say, i have a button and i want to remove all button.Click events for it - i don't know how many of them was hooked to the...
9
by: Gummy | last post by:
Hello, I created a user control that has a ListBox and a RadioButtonList (and other stuff). The idea is that I put the user control on the ASPX page multiple times and each user control will...
11
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When...
7
by: RSH | last post by:
I am working through some design patterns, and one recurring theme for me is the need to be able to communicate between objects while promoting encapsulation and loose coupling between them. ...
7
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.