DivijM
DivijM

Reputation: 343

Why is there a memory leak in my program?

I have implemented a singly-linked-list where there is a SinglyList class, and struct node, node *head as private members of the class; and wrote a destructor to delete it. I used CRT library to check for memory leaks using _CrtDumpMemoryLeaks() method. When I debug the code, it shows in the debug console that memory leaks are found, which is strange since I wrote a destructor to delete it.

This is shown in the debug console:-

Here is the node class:-

struct node 
{
    int data = NULL;
    node*next = NULL;
};

Here is the destructor:-

SinglyList::~SinglyList()
{
    node*curr = head,*next;
    while(curr != NULL)
    {
        next = curr->next;
        delete curr;
        curr = next;
    }
}

I don't understand, how can there be memory leaks when all nodes are deleted, kindly help.

Edit:

SinglyList.hpp:-

#pragma once 
#include<iostream>

class SinglyList
{
private:
    struct node 
    {
        int data = NULL;
        node*next = NULL;
    };
    node*head;
public:
    SinglyList();
    void Append(const int&);
    void Prepend(const int&);
    void Print();
    void Insert(const int&,const int&);
    void Delete(const int&);
    void Reverse();
    bool isEmpty();
    ~SinglyList();
};

SinglyList::SinglyList()
{
    head = NULL;
}

SinglyList::~SinglyList()
{
    node*curr = head,*next;
    while(curr != NULL)
    {
        next = curr->next;
        delete curr;
        curr = next;
    }
}

void SinglyList::Append(const int&data)
{
    node*n = new node{data};
    if(!head)
    {
        head = n;
        return;
    }
    node*ptr = head;
    while(ptr->next)
        ptr = ptr->next;
    ptr->next = n;
}

void SinglyList::Prepend(const int&data)
{
    node*n = new node{data};
    if(!head)
    {
        head = n;
        return;
    }
    n->next = head;
    head = n;
}

void SinglyList::Print()
{
    if(!head)
        return;
    node*ptr = head;
    while(ptr)
    {
        std::cout<<ptr->data<<' ';
        ptr = ptr->next;
    }
    std::cout<<std::endl;
}

void SinglyList::Insert(const int&pos,const int&data)
{
    if(pos == 1)
    {
        Prepend(data);
        return;
    }
    node*n = new node{data};
    node*ptr = head;
    for(int i = 1;i!=pos-1;i++)
        ptr = ptr->next;
    n->next = ptr->next;
    ptr->next = n;
}

void SinglyList::Delete(const int&pos)
{
    if(!head)
        return;
    node*ptr = head;
    if(pos == 1)
    {
        head= head->next;
        delete ptr;
        return;
    }
    for(int i = 1;i!= pos-1;i++)
        ptr = ptr->next;
    node*temp = ptr->next;
    ptr->next = temp->next;
    delete temp;
}

void SinglyList::Reverse()
{
    if(!head)
        return;
    node*curr = head,*prev = NULL,*next = NULL;
    while(curr)
    {
        next = curr->next;
        curr->next = prev;
        prev = curr;
        curr = next;
    }
    head = prev;
}

bool SinglyList::isEmpty()
{
    return (!head);
}

main.cpp:-

#include"SinglyList.hpp"
#include<crtdbg.h>

int main()
{
    SinglyList nums{};
    nums.Append(10);
    nums.Append(20);
    nums.Append(30);
    _CrtDumpMemoryLeaks();
    return 0;
}

Upvotes: 0

Views: 134

Answers (1)

Jeffrey
Jeffrey

Reputation: 11410

You print the leaks before the list gets deleted. Try this instead:

#include"SinglyList.hpp"
#include<crtdbg.h>

int main()
{
    {
        SinglyList nums{};
        nums.Append(10);
        nums.Append(20);
        nums.Append(30);
    }

    _CrtDumpMemoryLeaks();
    return 0;
}

Then, you'll show the actual leaks after your list gets destroyed.

Upvotes: 7

Related Questions