T
tjohnson
Assuming a class contains an event handler (say from a timer), and -
upon the event being caught - deletes itself from a dictionary, is this
legal? Will the GC ever clean this up (a breakpoint on the Dispose
method below never gets called, and a memory profile seems to show the
GC age is always zero).
The following code sample illustrates this scenario:
public class foo {
public static Dictionary<string,Bar> barDict;
...
public void CreateBar( string ID )
{
barDict.Add(ID, new Bar(ID));
}
}
public class Bar : IDisposable
{
public string key;
private Timer myTimer = new Timer();;
public Bar(string id)
{
key = id;
myTimer.Interval = 2000; myTimer.AutoReset = false;
myTimer.Elapsed += new
System.Timers.ElapsedEventHandler(timeout_Elapsed);
myTimer.Start();
}
void timeout_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
foo.barDict.Remove(key);
}
public void Dispose()
{
}
}
upon the event being caught - deletes itself from a dictionary, is this
legal? Will the GC ever clean this up (a breakpoint on the Dispose
method below never gets called, and a memory profile seems to show the
GC age is always zero).
The following code sample illustrates this scenario:
public class foo {
public static Dictionary<string,Bar> barDict;
...
public void CreateBar( string ID )
{
barDict.Add(ID, new Bar(ID));
}
}
public class Bar : IDisposable
{
public string key;
private Timer myTimer = new Timer();;
public Bar(string id)
{
key = id;
myTimer.Interval = 2000; myTimer.AutoReset = false;
myTimer.Elapsed += new
System.Timers.ElapsedEventHandler(timeout_Elapsed);
myTimer.Start();
}
void timeout_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
foo.barDict.Remove(key);
}
public void Dispose()
{
}
}