-2

I am using visual studio 2019 and _CrtDumpMemoryLeaks(); and I get the following message every time

Detected memory leaks!  
Dumping objects ->  
{160} normal block at 0x007D25A0, 8 bytes long. 
Data: <        > 04 00 00 00 00 00 00 00  
{159} normal block at 0x007D21B0, 8 bytes long. 
Data: <     %} > 03 00 00 00 A0 25 7D 00   
{158} normal block at 0x007D2290, 8 bytes long. 
Data: <     !} > 02 00 00 00 B0 21 7D 00   
{157} normal block at 0x007D2258, 8 bytes long. 
Data: <     "} > 01 00 00 00 90 22 7D 00  
Object dump complete.

The code:

  • main

    #define _CRTDBG_MAP_ALLOC
    #include <crtdbg.h>
    #include <iostream>
    
    #include "SingleLinkedList.h"
    
    int main()
    {
        s_list::SingleLinkedList list;
        list.InsertEnd(1);
        list.InsertEnd(2);
        list.InsertEnd(3);
        list.InsertEnd(4);
        list.Print();
    
        _CrtDumpMemoryLeaks();
    }
    
  • SingleLinkedList.h

    #pragma once
    
    namespace s_list {
    
        struct Node {
            int data;
            Node* next;
        };
    
        class SingleLinkedList
        {
        private:
            Node* head, *tail;
    
        public:
            SingleLinkedList();
            ~SingleLinkedList();
    
            void InsertEnd(int value);
            void InsertEnd(Node* node);
            void Print();
    
        };
    }
    
  • SingleLinkedList.cpp

    #include "SingleLinkedList.h"
    
    #include <iostream>
    
    using namespace s_list;
    using namespace std;
    
    SingleLinkedList::SingleLinkedList()
    {
        head = NULL;
        tail = NULL;
    }
    
    SingleLinkedList::~SingleLinkedList()
    {
        Node* iterator = head, *next;
        while (iterator != NULL)
        {
            next = iterator->next;
            delete iterator;
            iterator = next;
        }
    }
    
    void SingleLinkedList::InsertEnd(int val) {
        Node* newNode = new Node;
        newNode->data = val;
        newNode->next = NULL;
    
        if (head == NULL) 
        {
            head = newNode;
            tail = newNode;
        }
        else
        {
            tail->next = newNode;
            tail = newNode;
        }
    }
    

I think the issue lies in the destructor, and I also tried freeing the memory with free(iterator); but it produces the same result. I scrapped the unrelated part from the code, I hope it helps. And I also looked at other questions related to this issue, but I could not find anything that helped.

Bush
  • 1
  • 2
  • 1
    You’re calling the dump before destroying the list. The memory is still allocated and assumed it’s a leak. The destructor will only be called after `main` exits. Either put it into a separate block so the object will be destroyed after it, or a function. – Sami Kuhmonen Jun 20 '21 at 09:47
  • so it will be deallocated when exits the main block? – Bush Jun 20 '21 at 09:49
  • 2
    [Null vs nullptr. Why was it replaced](https://stackoverflow.com/questions/20509734/null-vs-nullptr-why-was-it-replaced) - as a sidenote. – rawrex Jun 20 '21 at 09:50
  • You forgot to include the definition of `Print` so the code doesn't compile. Not a big deal but for the future try to post valid code which can be tested really quickly. You'll get more answers this way. The best is when the entire code is one file. – Piotr Siupa Jun 20 '21 at 10:09

1 Answers1

1

Replace your main.cpp with the following one works.

The reason of your memory leak is that the real destruction happens at the very end of the code block of main, which is latter than your _CrtDumpMemoryLeaks() check.

#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#include <iostream>

#include "SingleLinkedList.h"

int main()
{
    {
        s_list::SingleLinkedList list;
        list.InsertEnd(1);
        list.InsertEnd(2);
        list.InsertEnd(3);
        list.InsertEnd(4);
        list.Print();
    }

    _CrtDumpMemoryLeaks();
}