Question on Hashtables
-
My understanding of Hashtables is this: If I declare a Hashtable like so: Hashtable *hashMyHashtable = new Hashtable; I don't need to have a corresponding DELETE after having used the NEW for the Hashtable. The reason being that the Hashtable is a Managed object and so I don't need to Delete it in my destructor. My question is this: If I store an object that was declared with a NEW, and store it in the hashtable, do I need to go back through the hashtable and delete those objects, or does the managed code do that as well? Example: MyObject *tempObject = new MyObject; Hashtable *hashObjects = new Hashtable; hashObjects->Add(key, tempObject); So if I store several tempObjects in my hashtable, do I need to traverse through the hashtable and DELETE the tempObjects, or does the garbage collector handle this as well as the hashtable itself? If I do need to delete them, will a call to hashObjects->clear(); delete the objects? Thanks, Jody Blau
-
My understanding of Hashtables is this: If I declare a Hashtable like so: Hashtable *hashMyHashtable = new Hashtable; I don't need to have a corresponding DELETE after having used the NEW for the Hashtable. The reason being that the Hashtable is a Managed object and so I don't need to Delete it in my destructor. My question is this: If I store an object that was declared with a NEW, and store it in the hashtable, do I need to go back through the hashtable and delete those objects, or does the managed code do that as well? Example: MyObject *tempObject = new MyObject; Hashtable *hashObjects = new Hashtable; hashObjects->Add(key, tempObject); So if I store several tempObjects in my hashtable, do I need to traverse through the hashtable and DELETE the tempObjects, or does the garbage collector handle this as well as the hashtable itself? If I do need to delete them, will a call to hashObjects->clear(); delete the objects? Thanks, Jody Blau
The correct way to work with managed objects in C++/CLI is to use managed references, and the gcnew operator.
//note the "^", which denotes a managed reference, just as a "*" denotes a pointer,
//and the use of the gcnew operator to instantiate the objects on the managed heap.
MyObject^ tempObject = gcnew MyObject();
Hashtable^ hashObjects = gcnew Hashtable();hashObjects->Add(key, tempObject);
Once all references to a managed object are set to null or go out of scope, the object will be garbage collected at whatever point the CLR decides to do so.
-
My understanding of Hashtables is this: If I declare a Hashtable like so: Hashtable *hashMyHashtable = new Hashtable; I don't need to have a corresponding DELETE after having used the NEW for the Hashtable. The reason being that the Hashtable is a Managed object and so I don't need to Delete it in my destructor. My question is this: If I store an object that was declared with a NEW, and store it in the hashtable, do I need to go back through the hashtable and delete those objects, or does the managed code do that as well? Example: MyObject *tempObject = new MyObject; Hashtable *hashObjects = new Hashtable; hashObjects->Add(key, tempObject); So if I store several tempObjects in my hashtable, do I need to traverse through the hashtable and DELETE the tempObjects, or does the garbage collector handle this as well as the hashtable itself? If I do need to delete them, will a call to hashObjects->clear(); delete the objects? Thanks, Jody Blau
jblau wrote:
do I need to go back through the hashtable and delete those objects
No, and you can see this by counting the references to the objects.
MyObject* tempObject = new MyObject;
The ref count on the object is now 1.
hashObjects->Add(key, tempObject);
The hash table now holds another reference on the object, so the ref count is 2. Once the
tempObject
object falls out of scope, the ref count drops to 1. When you empty the hash table, it releases the references on the objects that were in the table, so the ref count drops to 0. --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | NEW!! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Laugh it up, fuzzball. -
The correct way to work with managed objects in C++/CLI is to use managed references, and the gcnew operator.
//note the "^", which denotes a managed reference, just as a "*" denotes a pointer,
//and the use of the gcnew operator to instantiate the objects on the managed heap.
MyObject^ tempObject = gcnew MyObject();
Hashtable^ hashObjects = gcnew Hashtable();hashObjects->Add(key, tempObject);
Once all references to a managed object are set to null or go out of scope, the object will be garbage collected at whatever point the CLR decides to do so.
Justin I believe he's using the now obsolete MC++ syntax.
My blog : Nish’s thoughts on MFC, C++/CLI and .NET
-
jblau wrote:
do I need to go back through the hashtable and delete those objects
No, and you can see this by counting the references to the objects.
MyObject* tempObject = new MyObject;
The ref count on the object is now 1.
hashObjects->Add(key, tempObject);
The hash table now holds another reference on the object, so the ref count is 2. Once the
tempObject
object falls out of scope, the ref count drops to 1. When you empty the hash table, it releases the references on the objects that were in the table, so the ref count drops to 0. --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | NEW!! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Laugh it up, fuzzball.To add to what Mike says, the objects that you add to the hash table are also managed objects, and thus garbage collected. If you add unmanaged pointers to the hashtable, you'd have to manually delete the native objects that are pointed to by those pointers.
My blog : Nish’s thoughts on MFC, C++/CLI and .NET