By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,247 Members | 1,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,247 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.