-1

Hi I am having trouble with 2 memory leaks I just cant seem to fix I have commented where in the code the leak seems to be the problem.

Header main.cpp

#include <iostream>
#include <string>
#include "MyList.h"

// NODE FUNCTIONS //

template<class T>
void MyList<T>::push_front(T & newData)
{
    Node<T> *temp = new Node<T>; // CAUSES MEMORY LEAK... delete temp does not work
    temp->setData(newData);
    temp->setNext(NULL);
    if (isEmpty() == true)
    {

        temp->setNext(head);
        head = temp;
        tail = head;
    }
    else
    {

        temp->setNext(head);
        head = temp;
    }

}

template<class T>
void MyList<T>::push_back(T & newData)
{
    Node<T> *temp = new Node<T>; // CAUSES MEMORY LEAK... delete temp does not work
    temp->setData(newData);
    temp->setNext(NULL);
    if (isEmpty() == true)
    {
        temp->setNext(head);
        head = temp;
        tail = head;
    }
    else
    {
        tail->setNext(temp);
        tail = temp;
    }

}

template<class T>
void MyList<T>::pop_front()
{

    if (isEmpty() == true)
    {
        std::cout << "Nothing to pop" << std::endl;
    }
    else
    {
        Node<T> *temp = head;
        head = temp->getNext();

    }
}

template<class T>
void MyList<T>::pop_back()
{
    if (isEmpty() == true)
    {
        std::cout << "Nothing to pop" << std::endl;
    }
    else if (head == tail)
    {
        head = tail = NULL;
    }
    else
    {
        Node<T> *temp = head;

        while (temp->getNext() != tail)
        {
            temp = temp->getNext();
        }
        temp->setNext(NULL);
        tail = temp;
    }

}

I have tried changing the problem area to: Node *temp; that however does not work since its not initialized. I have tried changing my deconstructor many times to no change, I am completely lost since the code is working great and the list prints fine I just have 2 memory leak issues

C6912
  • 27
  • 1
  • 9
  • 1
    Yes, you have 2 memory leak issues in `pop_front()` and `pop_back()`, not where the comments are. – MikeCAT May 15 '16 at 08:22
  • 1
    If you're creating `temp` to push it into your list, why would you want to `delete` it? – sim642 May 15 '16 at 08:22
  • Hi MikeCAT can you perhaps point me in the right direction where the memory leaks are happening? – C6912 May 15 '16 at 08:24

1 Answers1

0

The instant solution for your problem (without even deeply understanding your design) is to use smart pointers which already exist in c++11 standard.

If you don't use c++11 you can implement your own smart pointers using RAII (example is here)

EDIT

Fix proposal for the current code:

template<class T>
void MyList<T>::pop_front()
{

    if (isEmpty() == true)
    {
        std::cout << "Nothing to pop" << std::endl;
    }
    else
    {
        Node<T> *temp = head;
        head = temp->getNext();
        delete temp; // <-- remove the ORIGINAL head from the memory
    }
}

template<class T>
void MyList<T>::pop_back()
{
    if (isEmpty() == true)
    {
        std::cout << "Nothing to pop" << std::endl;
    }
    else if (head == tail)
    {
        head = tail = NULL;
    }
    else
    {
        Node<T> *temp = head;

        while (temp->getNext() != tail)
        {
            temp = temp->getNext();
        }
        temp->setNext(NULL);
        delete tail; // <-- removing the ORIGINAL tail from the memory
        tail = temp;
    }
}
Community
  • 1
  • 1
Alex Lop.
  • 6,810
  • 1
  • 26
  • 45
  • Sorry thats not an option. I would like to try and understand why the memory leak is happening. – C6912 May 15 '16 at 08:32
  • @CryptiK Your problem is in `pop_front` and `pop_back` when you remove the content of the list BUT you don't `delete` it from the memory. Do you want me to add the "proposal" for the solution in my answer above? – Alex Lop. May 15 '16 at 08:35
  • @CryptiK Please see the EDIT section. – Alex Lop. May 15 '16 at 08:44
  • Thank you very much Ill give it a go. – C6912 May 15 '16 at 08:45
  • @CryptiK NP. Let me know if you still have mem leaks. – Alex Lop. May 15 '16 at 08:48
  • Yep it worked thanks. It threw me off because my memory leak check was saying that it was in the push not pop thanks again. – C6912 May 15 '16 at 09:51
  • @CryptiK You are welcome. It usually shows the code that allocates the lost memory chunks, not the place where it gets lost. – Alex Lop. May 15 '16 at 10:05