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

Weakreference Multicast Delegates

Having to remember to unregister for events to prevent a ref count seems to
be quite a burden to place on a developer. I have reviewed the
WeakMulticastDelegate solution proposed by Xavier Musy
(http://www.seedindustries.com/blog/x...1_archive.html) and Greg
Schechter’s Avalon solution.

1) Is there a more general solution in C#?

2) Why are delegates not weakreferences? It would seem a logical decision to
make - if the only reference to an object is as an event handler it would
seem that it should be GC'd. Weak references would allow for this.

Andrew

Nov 16 '05 #1
6 3441
Hi Andrew,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you would like the event handlers to be
GCed automantically. If there is any misunderstanding, please feel free to
let me know.

Based on the code you have provided, I think using WeakReference might
cause the event handler collected by GC at any time, which prevents the
handler from being called. It might make the app unstable.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #2
An interesting point, Kevin, but I'm trying now to think of a situation
in which I've created an object that listens for events from elsewhere
but has no other references to it, anywhere.

The only thing I can think of that ever came close were a bunch of data
binding classes that I wrote a while back. They subscribe to events on
two sides, and hold references to other objects, but don't _have_ to be
referenced by any other object in order to do their work. Regardless, I
made an ArrayList called "bindings" in the form that created them,
precisely because I didn't trust the delegate lists to keep them out of
the clutches of the GC. So, even if delegates had used weak references
I would still be safe.

Could you give an example of an object that must be kept "alive" even
though there is no reference to it but a delegate? Even DataBindings
have a list in the owning form that holds on to them....

Nov 16 '05 #3
"Bruce Wood" wrote:
An interesting point, Kevin, but I'm trying now to think of a situation
in which I've created an object that listens for events from elsewhere
but has no other references to it, anywhere.

The only thing I can think of that ever came close were a bunch of data
binding classes that I wrote a while back. They subscribe to events on
two sides, and hold references to other objects, but don't _have_ to be
referenced by any other object in order to do their work. Regardless, I
made an ArrayList called "bindings" in the form that created them,
precisely because I didn't trust the delegate lists to keep them out of
the clutches of the GC. So, even if delegates had used weak references
I would still be safe.

Could you give an example of an object that must be kept "alive" even
though there is no reference to it but a delegate? Even DataBindings
have a list in the owning form that holds on to them....


yes, anonymous methods in C# 2.0 comes to mind.
Nov 16 '05 #4

"Kevin Yu [MSFT]" <v-****@online.microsoft.com> wrote in message
news:gd**************@cpmsftngxa10.phx.gbl...
Hi Andrew,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you would like the event handlers to
be
GCed automantically. If there is any misunderstanding, please feel free to
let me know.

Based on the code you have provided, I think using WeakReference might
cause the event handler collected by GC at any time, which prevents the
handler from being called. It might make the app unstable.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."


Almost. The situation is this. We have a lot of long running singleton
objects - they are called "Manager" objects. One such object is a data
source that fires off events to anyone who is interested when a given set of
data is modified. There are a series of UI based controls that sit around
and render the data when the user has them on the screen. When the controls
are initialized they are hooked up to the "ChangedData" event via the +=
operator. The code is pretty standard:

dataPublisher.ChangedData += new ChangedDataHandler (...);

of course this means I cannot easily -= from the multicast delegate since I
don't keep a reference to the new ChangedDataHandler around. I could have
written:

handler = new ChangedDataHandler(...);
dataPublisher.ChangedData += handler;

and then during the closing of the control:

dataPublisher.ChangedData -= handler;

However this seems to be a lot of code - and in our specific situation we
would need to keep a LOT of delegate variables around only to be used during
unregistration. Instead, I think it is safe to let the MulticastDelegate
check to ensure that the registered objects actually are still valid, and
keep the reference as a WeakReference so that when the consumer of the data
is disposed/closed it is GC'd.

I'm not sure why this would introduce instabilities.

Thanks

Andrew
Nov 16 '05 #5
In article <uD**************@TK2MSFTNGP14.phx.gbl>,
An*********@nospam.nospam says...

"Kevin Yu [MSFT]" <v-****@online.microsoft.com> wrote in message
news:gd**************@cpmsftngxa10.phx.gbl...
Hi Andrew,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you would like the event handlers to
be
GCed automantically. If there is any misunderstanding, please feel free to
let me know.

Based on the code you have provided, I think using WeakReference might
cause the event handler collected by GC at any time, which prevents the
handler from being called. It might make the app unstable.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."


Almost. The situation is this. We have a lot of long running singleton
objects - they are called "Manager" objects. One such object is a data
source that fires off events to anyone who is interested when a given set of
data is modified. There are a series of UI based controls that sit around
and render the data when the user has them on the screen. When the controls
are initialized they are hooked up to the "ChangedData" event via the +=
operator. The code is pretty standard:

dataPublisher.ChangedData += new ChangedDataHandler (...);

of course this means I cannot easily -= from the multicast delegate since I
don't keep a reference to the new ChangedDataHandler around. I could have
written:

handler = new ChangedDataHandler(...);
dataPublisher.ChangedData += handler;

and then during the closing of the control:

dataPublisher.ChangedData -= handler;


This should work, no need to keep a reference. At least in normal
situation. It does not work with remoting though (at least for me I
needed to keep a reference as you show).

dataPublisher.ChangedData += new ChangedDataHandler(this.Handler);
dataPublisher.ChangedData -= new ChangedDataHandler(this.Handler);

Sunny
Nov 16 '05 #6
I have a similar situation, although I never considered the garbage
collection problem.

Could you not put the de-registration code in the Dispose methods of
the UI components, and Dispose them when they are no longer useful? I
haven't used Dispose very much, but it strikes me that that is the
"correct" C# idiom for this situation.

Of course, weak-reference delegates just make the whole thing a lot
easier and less error-prone....

Nov 16 '05 #7

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

Similar topics

0
by: IceShock | last post by:
I have been studying the C# language for sometime now and i have noticed something about delegates. When are single cast delegates to be used? even a better question: How do we define a singlecast...
1
by: Christoph Nahr | last post by:
The task: I want to let a background thread and a Windows Forms foreground thread communicate via callback methods. Now synchronization of a *single* delegate works just fine with the usual...
1
by: Hiten | last post by:
Hi I have created multicast delegates, now i want to point to perticular reference from delegate to invoke that mathod how do i do that please any one know it..... Thanks --- Hitendra
5
by: GVN | last post by:
Hi All, I recently worked on delegates. But I have a question regarding Multicast delegates. The scenario is as follows: I have a delegate myDelegate with object dlgt, and two methods...
7
by: pietro.cerutti | last post by:
Hi guys, I have a daemon running on Debian and listening for multicast packets sent to 224.0.0.251:5353 (ZeroConf Multicast DNS queries). The server is plugged into a VLAN trunk with eth0 and...
2
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
What are the different features (in VB.NET) between a) Custom Events and b) Multicast Delegates to raise events? On the bottom line my class ad a) manages a list of handlers or ad b) obtains a...
12
by: Pieter | last post by:
Hi, We have written a cache, which can have different behaviours. One of these is a WeakReference-Cache. The purpose is that, once an object isn't referred anymore in the application, it should...
5
by: AliRezaGoogle | last post by:
Hi, I have a conceptual question on Events and Multicast Delegates. Let me explain: As we know an event is a multicast delegate. What we declare as an event is inherently a multicast delegate....
9
by: raylopez99 | last post by:
Hello all— I’m trying to get the below to work and cannot get the format right. It’s from this example: http://msdn.microsoft.com/en-us/library/8627sbea(VS.71).aspx What it is: I’m trying...
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: 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...
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
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
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.