473,405 Members | 2,187 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,405 software developers and data experts.

Deleting ones self from a Dictionary

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,BarbarDict;
...
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()
{
}
}

Oct 26 '06 #1
1 1887
tjohnson,

Yes, that's perfectly legal and the GC will eventually cleanup the Bar
object (assuming it's not referenced anywhere else). The public
Dispose method is never called by the GC so that's why you're not
seeing your breakpoint hit there. Add a destructor and put a
breakpoint there.

Brian

tj******@broadcom.com wrotee
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,BarbarDict;
...
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()
{
}
}
Oct 26 '06 #2

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

Similar topics

2
by: Marc | last post by:
Hi all, I was using Tkinter.IntVar() to store values from a large list of parts that I pulled from a list. This is the code to initialize the instances: def initVariables(self): self.e =...
2
by: Thomas Philips | last post by:
I'm teaching myself OOP using Michael Dawson's "Python Programming For The Absolute Beginner" and have a question about deleting objects. My game has two classes: Player and Alien, essentially...
6
by: Thomas Philips | last post by:
I have a question about deleting objects. My game has two classes, Player and Alien, essentially identical, instances of which can shoot at each other. Player is described below class...
1
by: Alexander Kervero | last post by:
Hi ,today i was reading diveinto python book,in chapter 5 it has a very generic module to get file information,html,mp3s ,etc. The code of the example is here :...
2
by: KraftDiner | last post by:
I have a list, and within it, objects are marked for deletion. However when I iterate through the list to remove objects not all the marked objects are deleted.. here is a code portion: i = 0...
9
by: potholer | last post by:
I have been asked to create a self deleting exe, it has been a curious task that has proved fruitless I have searched in various places but to no avail I can do it in vb6 using a batch file,...
84
by: braver | last post by:
Is there any trick to get rid of having to type the annoying, character-eating "self." prefix everywhere in a class? Sometimes I avoid OO just not to deal with its verbosity. In fact, I try to...
4
AnuSumesh
by: AnuSumesh | last post by:
Hi I want that if user is logged in on one machine and trying to log in another machine also then he should not be allowed to log in again. For that-- i am creating Dictionary object on...
0
by: Andy B | last post by:
I have the following gridview. There are 2 other textBoxes and a button on the page as well. When somebody puts text into the 2 textBoxes and presses the add button, it puts the values of those...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.