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

Verifying object's events are not being observed before disposing

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 object's
events have been deregistered when it's time to delete said object.
If some other object is registered for one of his events, he will not
get garbage collected and just lays around, receiving that event.

What I was hoping I could do would be to write a utility class that
could use Reflection on any object, walk over that object's events,
and then check to see if each event has a registered handler. This
does not appear to be doable through the means I've investigated
(Type.GetEvents(), EventInfo, EventDescriptor, etc.).

Does anyone have a good suggestion about how I could go about doing
this? Any strategies you've used to ensure event handlers are
deregistered when necessary?

Thank you all,
Bryan
Nov 15 '05 #1
7 4056
Bryan D. wrote:
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 object's
events have been deregistered when it's time to delete said object.
If some other object is registered for one of his events, he will not
get garbage collected and just lays around, receiving that event.
I'm not entirely sure I understand what the problem is. Do you want one or
more of the subscribers to be GCed automatically when they are no longer
needed or is there a problem with publisher staying around (or both)?
What I was hoping I could do would be to write a utility class that
could use Reflection on any object, walk over that object's events,
and then check to see if each event has a registered handler. This
does not appear to be doable through the means I've investigated
(Type.GetEvents(), EventInfo, EventDescriptor, etc.).
AFAICT, reflection will only give you information about *classes* but not
objects. You want to know which object consumes events of which other
object.
Does anyone have a good suggestion about how I could go about doing
this? Any strategies you've used to ensure event handlers are
deregistered when necessary?


WeakReferences and dynamic invoke come to mind, but first we need to know
more accurately what your problem is. A short example program is always
best.

Regards,

Andreas

Nov 15 '05 #2
Bryan,
You do not need to use Reflection for this.

Remember that Events are implemented in terms of Delegates.

The System.Delegate class has a GetInvocationList method that returns a list
of all the handlers on the delegate.

You could check this invocation list to see if its empty, if not you could
empty it.

I don't have a sample, I would think setting the underlying delegate for the
event to null would clear the list.

Hope this helps
Jay

"Bryan D." <bd******@hotmail.com> wrote in message
news:34**************************@posting.google.c om...
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 object's
events have been deregistered when it's time to delete said object.
If some other object is registered for one of his events, he will not
get garbage collected and just lays around, receiving that event.

What I was hoping I could do would be to write a utility class that
could use Reflection on any object, walk over that object's events,
and then check to see if each event has a registered handler. This
does not appear to be doable through the means I've investigated
(Type.GetEvents(), EventInfo, EventDescriptor, etc.).

Does anyone have a good suggestion about how I could go about doing
this? Any strategies you've used to ensure event handlers are
deregistered when necessary?

Thank you all,
Bryan

Nov 15 '05 #3
Thank you both for your replies. Let me try and restate my problem, I
think it was confusing because I wasn't sure how to go about the
problem.

Our application is a long-running app, possibly for days at a time, so
we want to make very sure that objects that are no longer being used are
actually getting properly disposed and garbage collected.

The issue that we've run into is that when an object is disposed it
needs to deregister all of it's event handlers, otherwise it won't get
garbage collected, even though the thing is out of scope and "disposed".

I believe that my first email had me mixed up and was thus confusing. I
guess what I really want to do is look at all of the Delegates on the
object I'm being disposed and see if their invocation list is empty, ala
Jay's suggestion. Reading that, it makes perfect sense, and that's what
I really want to do.

Thanks for the replies, and sorry for the confusing post.
Cheers!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #4
Bryan Dotzour wrote:
Thank you both for your replies. Let me try and restate my problem, I
think it was confusing because I wasn't sure how to go about the
problem.

Our application is a long-running app, possibly for days at a time, so
we want to make very sure that objects that are no longer being used
are actually getting properly disposed and garbage collected.

The issue that we've run into is that when an object is disposed it
needs to deregister all of it's event handlers, otherwise it won't get
garbage collected, even though the thing is out of scope and
"disposed".


IIUC, then the problem is that the subscriber lives longer than you want, as
in:

delegate void MyEventHandler();

sealed class Publisher
{
public event MyEventHandler SomethingHappened;

public void Publish()
{
this.SomethingHappened();
}
}

sealed class Subscriber : IDisposable
{
private readonly Publisher publisher;

public Subscriber( Publisher publisher )
{
this.publisher = publisher;
publisher.SomethingHappened +=
new MyEventHandler( publisher_SomethingHappened );
}

public void Dispose()
{
publisher.SomethingHappened -=
new MyEventHandler( publisher_SomethingHappened );
}

private void publisher_SomethingHappened()
{
// Whatever
}
}

sealed class MyMain
{
static void Main()
{
Publisher publisher = new Publisher();
Subscriber subscriber = new Subscriber( publisher );
publisher.Publish();
// *** This does not make the subscriber go away ***
subscriber = null;

// more code
}
}
If this is the case, then one option is to make the subscriber disposable,
as I've done above. You would then simply call subscriber.Dispose() before
throwing it away and leave the rest to the garbage collector...

Regards,

Andreas

Nov 15 '05 #5
Andreas,
If this is the case, then one option is to make the subscriber disposable,
as I've done above. You would then simply call subscriber.Dispose() before
throwing it away and leave the rest to the garbage collector...
That is the way I would handle it, however having the Publisher check its
delegates Invocation List would allow a sanity check to make sure all the
subscribers are de-subscribing properly...

I'm not certain I would clean the invocation list, as much as log/throw some
sort of exception that says, hey wait I have subscribers who are still
subscribed... Or Hey wait these subscribers are still subscribed...

Just a thought
Jay

"Andreas Huber" <ah****@gmx.net> wrote in message
news:3f********@news.swissonline.ch... Bryan Dotzour wrote:
Thank you both for your replies. Let me try and restate my problem, I
think it was confusing because I wasn't sure how to go about the
problem.

Our application is a long-running app, possibly for days at a time, so
we want to make very sure that objects that are no longer being used
are actually getting properly disposed and garbage collected.

The issue that we've run into is that when an object is disposed it
needs to deregister all of it's event handlers, otherwise it won't get
garbage collected, even though the thing is out of scope and
"disposed".
IIUC, then the problem is that the subscriber lives longer than you want,

as in:

delegate void MyEventHandler();

sealed class Publisher
{
public event MyEventHandler SomethingHappened;

public void Publish()
{
this.SomethingHappened();
}
}

sealed class Subscriber : IDisposable
{
private readonly Publisher publisher;

public Subscriber( Publisher publisher )
{
this.publisher = publisher;
publisher.SomethingHappened +=
new MyEventHandler( publisher_SomethingHappened );
}

public void Dispose()
{
publisher.SomethingHappened -=
new MyEventHandler( publisher_SomethingHappened );
}

private void publisher_SomethingHappened()
{
// Whatever
}
}

sealed class MyMain
{
static void Main()
{
Publisher publisher = new Publisher();
Subscriber subscriber = new Subscriber( publisher );
publisher.Publish();
// *** This does not make the subscriber go away ***
subscriber = null;

// more code
}
}
If this is the case, then one option is to make the subscriber disposable,
as I've done above. You would then simply call subscriber.Dispose() before
throwing it away and leave the rest to the garbage collector...

Regards,

Andreas

Nov 15 '05 #6
Jay B. Harlow [MVP - Outlook] wrote:
Andreas,
If this is the case, then one option is to make the subscriber
disposable, as I've done above. You would then simply call
subscriber.Dispose() before throwing it away and leave the rest to
the garbage collector...

That is the way I would handle it, however having the Publisher check
its delegates Invocation List would allow a sanity check to make sure
all the subscribers are de-subscribing properly...


You mean you'd make Publisher disposable too and in Dispose() make sure that
all subscribers are deregistered? That certainly sounds like a good idea.

Regards,

Andreas

Nov 15 '05 #7
Andreas,
You mean you'd make Publisher disposable too and in Dispose() make sure that all subscribers are deregistered? That certainly sounds like a good idea.
Yes.

Jay

"Andreas Huber" <ah****@gmx.net> wrote in message
news:3f********@news.swissonline.ch... Jay B. Harlow [MVP - Outlook] wrote:
Andreas,
If this is the case, then one option is to make the subscriber
disposable, as I've done above. You would then simply call
subscriber.Dispose() before throwing it away and leave the rest to
the garbage collector...
That is the way I would handle it, however having the Publisher check
its delegates Invocation List would allow a sanity check to make sure
all the subscribers are de-subscribing properly...


You mean you'd make Publisher disposable too and in Dispose() make sure

that all subscribers are deregistered? That certainly sounds like a good idea.

Regards,

Andreas

Nov 15 '05 #8

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

Similar topics

0
by: TP-Software | last post by:
Hi, I have a ChatFormCollection class which holds a collection of the class ChatForm which inherits from Form. MainForm has an instance of ChatFormCollection. When a ChatForm instance is...
2
by: Jon Davis | last post by:
The garbage handler in the .NET framework is handy. When objects fall out of scope, they are automatically destroyed, and the programmer doesn't have to worry about deallocating the memory space...
2
by: Dan | last post by:
I am having a problem trying to assign event handlers to the events of a remoted object. I have one program that registers the object for remoting, Another that connects and calls methods on the...
0
by: ChrisN | last post by:
Howdy, I'm writing a web application in VB.Net. I'm using C# for all of the dynamic content on the vb pages. I'm currently building a VB page that displays a table of pictures. The page calls...
0
by: Bob Trabucco | last post by:
Hello all, I am attempting to use a component written in VB.NET in our VB6 legacy app. Wrote the VB.NET app, gave it a interface and a strong name, regasm & gacutil on it. Works great when...
3
by: daan | last post by:
Hello, I have a problem and I can't get the solution for it :( I have a com dll, which i imported as a reference. The com object is part of a class which is multithreaded and will create...
2
by: stein | last post by:
I need to create a COM object (in C#) that both has methods a client can call and events that a client can receieve. The client will be written i C++. I have already made it possible to call...
1
by: mirandacascade | last post by:
Apologies in advance...I'm sure this is a trivial question... Access 97. Situation is this: 1. VBA code sets the recordsource property of a report object; the recordsource is a querydef which...
11
by: Jay Dee | last post by:
Object garbage collection of events I can not help wondering how the garbage collection system handles events. If I release all references to an object that has a void that an event somewhere...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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
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...

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.