469,347 Members | 19,743 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Memory Leak in C# 2.0 W/ Anonymous Delegates?

I believe I ran into an interesting way to create memory leaks in C# 2.0
using anymous delegates. Here is a sample of the code in question.

private void Handle_Event(object sender, EventArgs e)
{
Timer timer = new Timer();
timer.Interval = 10000;
NotifyForm notifyForm = new notifyForm();
notifyForm.Show();
timer.Tick += delegate(object timerSender, EventArgs tArgs)
{
notifyForm.Close();
notifyForm.Dispose();
timer.Stop();
};
timer.Start();
}

The above code is responding to an event, displaying a Form to notify the
user of the completed event and closing the Form in 10 seconds. The
potential issue I see is I assign an anonymous delegate into the timer's Tick
event. The timer's Tick event now has a reference to my anonymous delegate.
According to Anders Heilsbergs book on C# 2.0 the timer reference will now be
captured and will not be released until after the anonymous delegate has been
garbage collected, but in the above example the anonymous delegate cannot be
collected , because the Timer's Tick event has a reference to it. Is this
analysis of this code correct? If so what suggestions do you have to get
around this. I have my own suggestion, it goes something this (I really have
to credit my co-worker Mike for this suggestion):

private void Handle_Event(object sender, EventArgs e)
{
Timer timer = new Timer();
timer.Interval = 10000;
NotifyForm notifyForm = new notifyForm();
notifyForm.Show();
//Initializing the handler variable to null allows it to be accessed
inside of the
//anonymous delegate.
EventHandler handler = null;
handler = delegate(object timerSender, EventArgs tArgs)
{
notifyForm.Close();
notifyForm.Dispose();
timer.Stop();
timer.Tick -= handler;
} ;
timer.Tick += handler;
timer.Start();
}

The idea in the above example being that the anonymous delegate is assigned
into a local variable which can be accessed inside of the anonymous delegate.
By doing this I can unsubscribe from the Tick event allowing the delegate to
garbage collected, and consequently the Timer instance can also be garbage
collected. Does this solve the issues of the memory leak? Is there a better
way to solve this issue? Thank you.

Nov 17 '05 #1
3 8602

"anonymous" <an*******@discussions.microsoft.com> wrote in message
news:24**********************************@microsof t.com...
I believe I ran into an interesting way to create memory leaks in C# 2.0
using anymous delegates. Here is a sample of the code in question.

private void Handle_Event(object sender, EventArgs e)
{
Timer timer = new Timer();
timer.Interval = 10000;
NotifyForm notifyForm = new notifyForm();
notifyForm.Show();
timer.Tick += delegate(object timerSender, EventArgs tArgs)
{
notifyForm.Close();
notifyForm.Dispose();
timer.Stop();
};
timer.Start();
}

The above code is responding to an event, displaying a Form to notify the
user of the completed event and closing the Form in 10 seconds. The
potential issue I see is I assign an anonymous delegate into the timer's
Tick
event. The timer's Tick event now has a reference to my anonymous
delegate.
According to Anders Heilsbergs book on C# 2.0 the timer reference will now
be
captured and will not be released until after the anonymous delegate has
been
garbage collected, but in the above example the anonymous delegate cannot
be
collected , because the Timer's Tick event has a reference to it. Is this
analysis of this code correct? If so what suggestions do you have to get
around this. I have my own suggestion, it goes something this (I really
have
to credit my co-worker Mike for this suggestion):

This should collect just fine, since once the anonymous delegate is
unreferenced and it contains the only reference to the timer, then the timer
instance cannot be reached from a root and is therefore collectable as well.
Nov 17 '05 #2


"Daniel O'Connell [C# MVP]" wrote:

"anonymous" <an*******@discussions.microsoft.com> wrote in message
news:24**********************************@microsof t.com...
I believe I ran into an interesting way to create memory leaks in C# 2.0
using anymous delegates. Here is a sample of the code in question.

private void Handle_Event(object sender, EventArgs e)
{
Timer timer = new Timer();
timer.Interval = 10000;
NotifyForm notifyForm = new notifyForm();
notifyForm.Show();
timer.Tick += delegate(object timerSender, EventArgs tArgs)
{
notifyForm.Close();
notifyForm.Dispose();
timer.Stop();
};
timer.Start();
}

The above code is responding to an event, displaying a Form to notify the
user of the completed event and closing the Form in 10 seconds. The
potential issue I see is I assign an anonymous delegate into the timer's
Tick
event. The timer's Tick event now has a reference to my anonymous
delegate.
According to Anders Heilsbergs book on C# 2.0 the timer reference will now
be
captured and will not be released until after the anonymous delegate has
been
garbage collected, but in the above example the anonymous delegate cannot
be
collected , because the Timer's Tick event has a reference to it. Is this
analysis of this code correct? If so what suggestions do you have to get
around this. I have my own suggestion, it goes something this (I really
have
to credit my co-worker Mike for this suggestion):

This should collect just fine, since once the anonymous delegate is
unreferenced and it contains the only reference to the timer, then the timer
instance cannot be reached from a root and is therefore collectable as well.

You're saying that the original snippet will work just fine? If that's the
case then don't I run the risk of the timer and the delegate being garbaged
collected before the Tick event is fired, making it so my notify form remains
opened indefinately?
Nov 17 '05 #3


"anonymous" wrote:


"Daniel O'Connell [C# MVP]" wrote:

"anonymous" <an*******@discussions.microsoft.com> wrote in message
news:24**********************************@microsof t.com...
I believe I ran into an interesting way to create memory leaks in C# 2.0
using anymous delegates. Here is a sample of the code in question.

private void Handle_Event(object sender, EventArgs e)
{
Timer timer = new Timer();
timer.Interval = 10000;
NotifyForm notifyForm = new notifyForm();
notifyForm.Show();
timer.Tick += delegate(object timerSender, EventArgs tArgs)
{
notifyForm.Close();
notifyForm.Dispose();
timer.Stop();
};
timer.Start();
}

The above code is responding to an event, displaying a Form to notify the
user of the completed event and closing the Form in 10 seconds. The
potential issue I see is I assign an anonymous delegate into the timer's
Tick
event. The timer's Tick event now has a reference to my anonymous
delegate.
According to Anders Heilsbergs book on C# 2.0 the timer reference will now
be
captured and will not be released until after the anonymous delegate has
been
garbage collected, but in the above example the anonymous delegate cannot
be
collected , because the Timer's Tick event has a reference to it. Is this
analysis of this code correct? If so what suggestions do you have to get
around this. I have my own suggestion, it goes something this (I really
have
to credit my co-worker Mike for this suggestion):

This should collect just fine, since once the anonymous delegate is
unreferenced and it contains the only reference to the timer, then the timer
instance cannot be reached from a root and is therefore collectable as well.

You're saying that the original snippet will work just fine? If that's the
case then don't I run the risk of the timer and the delegate being garbaged
collected before the Tick event is fired, making it so my notify form remains
opened indefinately?

Well I peeked at the MSIL that is generated and I think I see what you're
saying. In the original snippet the timer is eligible for garbage
collection. This means that I run a serious risk of both my timer and
anonymous delegate getting garbage collected prior to the event even being
fired. The solution is to make the timer a class level member variable, not
a local variable. The reason for this is C# generates a nested class for the
anymous delegate that stores all of the referenced locals. The nested class
contains a method that has the actual implementation for the anonymous
delegate logic. That method is assigned in as a delegate to the Tick event.
Now I've got the timer referencing the instance of my anonymous delegate
nested class and the anonymous delegate instance reference the timer, but
they aren't reference by anyone else, making them eligible for garbage
collection.
Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by jong | last post: by
18 posts views Thread by Dennis | last post: by
10 posts views Thread by Matt Kruse | last post: by
5 posts views Thread by Aaron | last post: by
19 posts views Thread by Jon Davis | last post: by
reply views Thread by Gujju | last post: by
4 posts views Thread by Carlo | last post: by
4 posts views Thread by Frankie | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.