470,635 Members | 2,343 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,635 developers. It's quick & easy.

Events, weak references or other approach advice required

Hi, I've just written the following code to describe the issue I have.
In a nutshell if an object subscribes to an event on a long lived
object and I then ditch the reference to the subscriber (set it to
null) The object doesn't get GCed because the publisher still has a
reference to it.
Solutions I can think of are:
1) WeakReference - I'm not sure how to do that (Needs to be framework
1.1 and 2 compatible fyi) and I'd like to use events rather than
delegates directly if that makes sense.
2) Implement an interface to explicitly unsubscribe the subscribers
(Doesn't have to be an interface, just a method for simplicity) but
then I have to remember to call that. Not a huge hassle, and something
I already do in other applications.

Here's the code.

//Publisher first
-------------------------------------------------------
using System;
using System.Timers;

namespace Eventer
{
public delegate void TimerEventHandler(object sender, TimerEventArgs
e);
/// <summary>
/// Summary description for Publisher.
/// </summary>
public class Publisher
{
private Timer _timer=new Timer(2000);

public event TimerEventHandler TimerFired;

public Publisher()
{
_timer.Elapsed+=new ElapsedEventHandler(_timer_Elapsed);
_timer.Start();
}
private void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
TimerEventArgs nE=new TimerEventArgs(e.SignalTime.Ticks);
OnTimerFired(nE);
}
protected virtual void OnTimerFired(TimerEventArgs e)
{
if (null != TimerFired)
{
TimerFired(this,e);
}
}
}

public class TimerEventArgs : EventArgs
{
long _tick=0;

public TimerEventArgs(long pTick)
{
_tick = pTick;
}

public long Tick
{
get
{
return _tick;
}
}
}
}

// Subscriber
---------------------------------------------------------------------

using System;

namespace Eventer
{
/// <summary>
/// Summary description for Subscriber.
/// </summary>
public class Subscriber
{
int _value;

public Subscriber(int pValue, Publisher pPublisher)
{
_value = pValue;
pPublisher.TimerFired+=new TimerEventHandler(pPublisher_TimerFired);
}

private void pPublisher_TimerFired(object sender, TimerEventArgs e)
{
Console.WriteLine(_value.ToString() + " - " + e.Tick.ToString());
}
}
}

// The relevant bit of the form that tests it. Basically two buttons
one that starts and one that stops ------------------

private Publisher _publisber=new Publisher();
private System.Collections.ArrayList _arrayList=new ArrayList(5);

//Create subscribers
private void button1_Click(object sender, System.EventArgs e)
{
Subscriber s;

for(int c=0;c<5;c++)
{
s=new Subscriber(c,_publisber);
_arrayList.Add(s);
}

}

//clear reference to subscribers
private void button2_Click(object sender, System.EventArgs e)
{
_arrayList=null;
GC.Collect(); //trying to hurry things along for the demo
}

Sep 11 '06 #1
4 1454
One suggestion is to make the objects Disposable and then dispose them before
setting them to null. Then in the Dispose function you can un register them
from the event.
Ciaran O'Donnell

"DeveloperX" wrote:
Hi, I've just written the following code to describe the issue I have.
In a nutshell if an object subscribes to an event on a long lived
object and I then ditch the reference to the subscriber (set it to
null) The object doesn't get GCed because the publisher still has a
reference to it.
Solutions I can think of are:
1) WeakReference - I'm not sure how to do that (Needs to be framework
1.1 and 2 compatible fyi) and I'd like to use events rather than
delegates directly if that makes sense.
2) Implement an interface to explicitly unsubscribe the subscribers
(Doesn't have to be an interface, just a method for simplicity) but
then I have to remember to call that. Not a huge hassle, and something
I already do in other applications.

Here's the code.

//Publisher first
-------------------------------------------------------
using System;
using System.Timers;

namespace Eventer
{
public delegate void TimerEventHandler(object sender, TimerEventArgs
e);
/// <summary>
/// Summary description for Publisher.
/// </summary>
public class Publisher
{
private Timer _timer=new Timer(2000);

public event TimerEventHandler TimerFired;

public Publisher()
{
_timer.Elapsed+=new ElapsedEventHandler(_timer_Elapsed);
_timer.Start();
}
private void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
TimerEventArgs nE=new TimerEventArgs(e.SignalTime.Ticks);
OnTimerFired(nE);
}
protected virtual void OnTimerFired(TimerEventArgs e)
{
if (null != TimerFired)
{
TimerFired(this,e);
}
}
}

public class TimerEventArgs : EventArgs
{
long _tick=0;

public TimerEventArgs(long pTick)
{
_tick = pTick;
}

public long Tick
{
get
{
return _tick;
}
}
}
}

// Subscriber
---------------------------------------------------------------------

using System;

namespace Eventer
{
/// <summary>
/// Summary description for Subscriber.
/// </summary>
public class Subscriber
{
int _value;

public Subscriber(int pValue, Publisher pPublisher)
{
_value = pValue;
pPublisher.TimerFired+=new TimerEventHandler(pPublisher_TimerFired);
}

private void pPublisher_TimerFired(object sender, TimerEventArgs e)
{
Console.WriteLine(_value.ToString() + " - " + e.Tick.ToString());
}
}
}

// The relevant bit of the form that tests it. Basically two buttons
one that starts and one that stops ------------------

private Publisher _publisber=new Publisher();
private System.Collections.ArrayList _arrayList=new ArrayList(5);

//Create subscribers
private void button1_Click(object sender, System.EventArgs e)
{
Subscriber s;

for(int c=0;c<5;c++)
{
s=new Subscriber(c,_publisber);
_arrayList.Add(s);
}

}

//clear reference to subscribers
private void button2_Click(object sender, System.EventArgs e)
{
_arrayList=null;
GC.Collect(); //trying to hurry things along for the demo
}

Sep 11 '06 #2
try this:

System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\test.xml");
System.Xml.XmlTextWriter xtr = new System.Xml.XmlTextWriter(file);
xtr.Formatting = System.Xml.Formatting.Indented;
ser.Serialize(xtr, test);
file.Flush();
file.Close();
Ciaran O'Donnell
"DeveloperX" wrote:
Hi, I've just written the following code to describe the issue I have.
In a nutshell if an object subscribes to an event on a long lived
object and I then ditch the reference to the subscriber (set it to
null) The object doesn't get GCed because the publisher still has a
reference to it.
Solutions I can think of are:
1) WeakReference - I'm not sure how to do that (Needs to be framework
1.1 and 2 compatible fyi) and I'd like to use events rather than
delegates directly if that makes sense.
2) Implement an interface to explicitly unsubscribe the subscribers
(Doesn't have to be an interface, just a method for simplicity) but
then I have to remember to call that. Not a huge hassle, and something
I already do in other applications.

Here's the code.

//Publisher first
-------------------------------------------------------
using System;
using System.Timers;

namespace Eventer
{
public delegate void TimerEventHandler(object sender, TimerEventArgs
e);
/// <summary>
/// Summary description for Publisher.
/// </summary>
public class Publisher
{
private Timer _timer=new Timer(2000);

public event TimerEventHandler TimerFired;

public Publisher()
{
_timer.Elapsed+=new ElapsedEventHandler(_timer_Elapsed);
_timer.Start();
}
private void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
TimerEventArgs nE=new TimerEventArgs(e.SignalTime.Ticks);
OnTimerFired(nE);
}
protected virtual void OnTimerFired(TimerEventArgs e)
{
if (null != TimerFired)
{
TimerFired(this,e);
}
}
}

public class TimerEventArgs : EventArgs
{
long _tick=0;

public TimerEventArgs(long pTick)
{
_tick = pTick;
}

public long Tick
{
get
{
return _tick;
}
}
}
}

// Subscriber
---------------------------------------------------------------------

using System;

namespace Eventer
{
/// <summary>
/// Summary description for Subscriber.
/// </summary>
public class Subscriber
{
int _value;

public Subscriber(int pValue, Publisher pPublisher)
{
_value = pValue;
pPublisher.TimerFired+=new TimerEventHandler(pPublisher_TimerFired);
}

private void pPublisher_TimerFired(object sender, TimerEventArgs e)
{
Console.WriteLine(_value.ToString() + " - " + e.Tick.ToString());
}
}
}

// The relevant bit of the form that tests it. Basically two buttons
one that starts and one that stops ------------------

private Publisher _publisber=new Publisher();
private System.Collections.ArrayList _arrayList=new ArrayList(5);

//Create subscribers
private void button1_Click(object sender, System.EventArgs e)
{
Subscriber s;

for(int c=0;c<5;c++)
{
s=new Subscriber(c,_publisber);
_arrayList.Add(s);
}

}

//clear reference to subscribers
private void button2_Click(object sender, System.EventArgs e)
{
_arrayList=null;
GC.Collect(); //trying to hurry things along for the demo
}

Sep 11 '06 #3
Hi,
"DeveloperX" <nn*****@operamail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
Hi, I've just written the following code to describe the issue I have.
In a nutshell if an object subscribes to an event on a long lived
object and I then ditch the reference to the subscriber (set it to
null) The object doesn't get GCed because the publisher still has a
reference to it.
Solutions I can think of are:
1) WeakReference - I'm not sure how to do that (Needs to be framework
1.1 and 2 compatible fyi) and I'd like to use events rather than
delegates directly if that makes sense.
Not sure how (if) this will work.
2) Implement an interface to explicitly unsubscribe the subscribers
(Doesn't have to be an interface, just a method for simplicity) but
then I have to remember to call that. Not a huge hassle, and something
I already do in other applications.
I think this is the way to go with a twist, instead of a new interface you
could implement IDisposable and do your "cleaning" in the Dispose method.

You object will need a list of the subscriptions he did during its lifetime
and just unsubscribe from them. Not a terrible difficilt thing to implement
I think.

--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Sep 11 '06 #4

Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi,
"DeveloperX" <nn*****@operamail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
Hi, I've just written the following code to describe the issue I have.
In a nutshell if an object subscribes to an event on a long lived
object and I then ditch the reference to the subscriber (set it to
null) The object doesn't get GCed because the publisher still has a
reference to it.
Solutions I can think of are:
1) WeakReference - I'm not sure how to do that (Needs to be framework
1.1 and 2 compatible fyi) and I'd like to use events rather than
delegates directly if that makes sense.

Not sure how (if) this will work.
2) Implement an interface to explicitly unsubscribe the subscribers
(Doesn't have to be an interface, just a method for simplicity) but
then I have to remember to call that. Not a huge hassle, and something
I already do in other applications.

I think this is the way to go with a twist, instead of a new interface you
could implement IDisposable and do your "cleaning" in the Dispose method.

You object will need a list of the subscriptions he did during its lifetime
and just unsubscribe from them. Not a terrible difficilt thing to implement
I think.

--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Yep, I use an interface as I wrap up registering and unregistering in
the same place. The event thing just suprised me for some reason, I'm
suprised I've not noticed it in the past, but I guess it doesn't
necessarily come up that often.
I'm as likely to register the subscribers with the publisher and have
the publisher call methods on the subscribers. Same issue of course,
Publisher holds a reference to the subscriber, but there you go.

Sep 11 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by scsharma | last post: by
16 posts views Thread by anonymous.user0 | last post: by
30 posts views Thread by Burkhard | last post: by
5 posts views Thread by Daniel | last post: by
5 posts views Thread by Aleksey | last post: by
1 post views Thread by Henri.Chinasque | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.