Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 13th, 2007, 12:21 PM
Registered User
 
Join Date: Jun 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Hashtable and lifetime of objects inside

Hi all,

I would like to know how to be sure that calling the clear method of my hashtable frees memory from the objects inside.
For example I have:

MyHashTable.Add(mykey, new MyObject(...))
.....

and finally

MyHashTable.Clear(); //because I want to re-populate it again

This code erases all MyObject from memory?
If not, how can I do it?

Thanks
 
Old February 16th, 2007, 09:12 AM
Registered User
 
Join Date: Feb 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Ruffnekk
Default

The Garbage Collector will dispose the objects when it needs to, and that may not be immediately after you call Clear(). You could however loop through the collection and call Dispose() on each object manually if the objects implement IDisposable.

--
Regards,
Ruffnekk
--
Programming is an art that fights back.
 
Old February 16th, 2007, 08:25 PM
Authorized User
 
Join Date: Feb 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Be aware that the garbage collection only applies to items in the Hashtable that are not also referenced elsewhere. That is, sometime after *all* references to the item are removed the item is garbage collected.


 
Old February 19th, 2007, 07:33 AM
Registered User
 
Join Date: Jun 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks.

If I write a Dispose metod, what should I put in it to delete my object?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Hashtable ajit Java Basics 1 July 11th, 2006 01:25 AM
CCommand Lifetime comdev2001 Oracle 0 November 29th, 2005 12:13 PM
Hashtable Scott Rider General .NET 7 July 3rd, 2005 02:25 AM
Hashtable in C# sachin-csharp .NET Framework 2.0 0 July 28th, 2004 01:45 AM
Threads and an objects lifetime Kep C# 3 May 18th, 2004 01:11 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.