简体   繁体   中英

Memory leak in Hashtable/Doubly linked list

I'm working for my CS class and we are developing our own simple hashtable-implementation. For this purpose I created multiple classes:

  • Liste (This is a doubly linked list)
  • Hashtable (This is a simple hashtable with a flexible number of slots)
  • Tester (This is a testing class which counts the key-comparisons and prints them into a csv-file for a range of cases)

The Tester generates a new hashtable for every run. For example when I perform a test of 100 runs for every run a new hashtable will be created after the old one is deleted. This is needed because usually the number of slots changes for the tests.

In every slot of the hashtable is a pointer to a doubly linked list, which is created in the constructor of the hashtable. The list offers methods for inserting values, looking for values, get the key-comparinsons for the last search and clearing all elements. The destructor calls the method for clearing all elements in the doubly linked list. The destructor is called at the end of a run, I checked it with a debug message. I also tried to check if the same number of elements are cleared from memory that I created before, but the reference count always fits.

My problem is that after every run of the Tester more memory is allocated. If you do a lot of runs or runs with a high number of elements in the hashtable this is a real problem because it takes mega- or gigabytes of memory.

My IDE is the latest version of Xcode on OS X. I used Instruments (profiling tool) to look for the leaking code, but it just suggests to look at my add-method.

    void Liste::add(int key, int wert)
    {
        Element *createdElement = new Element();
        this->referenceCount++;

        createdElement->wert = wert;
        createdElement->key = key;

        if(this->head == NULL && this->tail == NULL)
        {
            this->head = createdElement;
            this->tail = createdElement;
        }
        else
        {
            tail->next = createdElement;
            createdElement->prev = this->tail;
            this->tail = createdElement;
        }

        this->size++;
    }

Of course this is the place where the memory is allocated, but my clear-method later should delete all those elements later in the game when it is fired by the destructor:

    void Liste::clear()
    {
        cout << "Liste::clear() is fired." << endl;
        Element *cursor = this->head;

        while (cursor != NULL)
        {
            if(cursor->prev != NULL)
            {
                delete cursor->prev;
                cursor->prev = NULL;
                this->referenceCount--;
            }

            cursor = cursor->next;
        }

        delete cursor;
        this->referenceCount--;

        this->head = NULL;
        this->tail = NULL;
        this->size = 0;
    }

After what Instruments is telling me the add-method is the only place memory is allocated that is not freed up after using it so I hope you can help me with this memory leak. I have some experience with multiple programming languages, but I never had this much trouble with memory management until I had to code in C++.

It looks like you are never deleting the last cursor since its previous will be null. If(cursor->prev!=null) ... Else Delete cursor; break;

You also don't need the other delete cursor call because it is always null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM