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