none
How to dispose the Hashtable enumerator fully after finish the loop? RRS feed

  • Question

  • How to dispose the Hashtable enumerator fully after finish the loop?

    thanks

    Edited: I had used hashtable.Clone() to make shallow copy everytime it enumerates


    • Edited by 6387534 Tuesday, July 2, 2013 8:26 AM edit
    Tuesday, July 2, 2013 8:00 AM

Answers

All replies

  • A Hashtable enumerator is not disposable.

    Tuesday, July 2, 2013 8:10 AM
    Moderator
  • A Hashtable enumerator is not disposable.

    Oh.. so how can i free the memory?

    Edited: I had used hashtable.Clone() to make shallow copy everytime it enumerates


    • Edited by 6387534 Tuesday, July 2, 2013 8:24 AM edit2
    Tuesday, July 2, 2013 8:20 AM
  • You can't free memory use by managed objects, the GC does that automatically.

    I suppose this is related to your other thread: http://social.msdn.microsoft.com/Forums/vstudio/en-US/a161399c-aec1-403d-9cdc-bb1130a2ec13/c-hashtable-getenumerator-bugs#30dff3b4-0e78-480b-bcde-086406c259eb

    I told you there that you don't have any proof that you have a memory leak.

    Tuesday, July 2, 2013 8:26 AM
    Moderator
  • Ok.. last question,

    What is the difference between

    IDictionaryEnumerator enumerator = GetHashtable(ClientList).GetEnumerator();
    
    GetHashtable:
    public Hashtable GetHashtable(Hashtable table)
    {
        return (Hashtable)table.Clone();
    }

    And

    IDictionaryEnumerator enumerator = ClientList.GetEnumerator();

    Is there any difference on memory usage?

    Tuesday, July 2, 2013 8:38 AM
  • "Is there any difference on memory usage?"

    Well, of course. Clone will "clone" the internal hashtable arrays so it allocates additional memory. At some future point in time that memory will be collected by the GC but until then it's "used memory".

    Tuesday, July 2, 2013 8:43 AM
    Moderator
  • If i need to change value during the enumeration, can i just use the latter code?

    Also, can I use foreach instead?

           foreach( DictionaryEntry de in ClientList )
            {
                 if(de == null)
                 {
                      ClientList.Remove(de);
                 }
            }


    • Edited by 6387534 Tuesday, July 2, 2013 9:06 AM edit
    Tuesday, July 2, 2013 9:04 AM
  • "If i need to change value during the enumeration, can i just use the latter code?"

    No. Any attempt at modifying the hashtable will invalidate the enumerator.

    "Also, can I use foreach instead?"

    You can use foreach but that doesn't change the "no change" restriction above.

    Tuesday, July 2, 2013 9:12 AM
    Moderator