Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Question on Hashtables

Question on Hashtables

Scheduled Pinned Locked Moved Managed C++/CLI
questiontutorial
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jblau
    wrote on last edited by
    #1

    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

    J M 2 Replies Last reply
    0
    • J jblau

      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

      J Offline
      J Offline
      J Dunlap
      wrote on last edited by
      #2

      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.

      N 1 Reply Last reply
      0
      • J jblau

        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

        M Offline
        M Offline
        Michael Dunn
        wrote on last edited by
        #3

        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.

        N 1 Reply Last reply
        0
        • J J Dunlap

          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.

          N Offline
          N Offline
          Nish Nishant
          wrote on last edited by
          #4

          Justin I believe he's using the now obsolete MC++ syntax.

          My blog : Nish’s thoughts on MFC, C++/CLI and .NET

          1 Reply Last reply
          0
          • M Michael Dunn

            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.

            N Offline
            N Offline
            Nish Nishant
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups