Let's try to fix the memory leak issue by adding the following code in the List::~List() destructor:
List::~List( ) {
cout << "\nList destructor invoked ..." << endl;
Node *pTemp = NULL;
while ( pHead != NULL ) {
pTemp = pHead;
pHead = pHead->next;
delete pTemp;
}
pNewNode = pHead = pTail = pTemp = NULL;
__size = 0;
}
From the following output, you will observe that the memory leak has been fixed:
g++ main.cpp list.cpp -std=c++17 -g
valgrind ./a.out --leak-check=full
==44813== Memcheck, a memory error detector
==44813== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==44813== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==44813== Command: ./a.out --leak-check=full
==44813==
List constructor invoked ...
List entries are ...
10 20 30 40 50
Memory utilised by the list is 80
List destructor invoked ...
==44813==
==44813== HEAP SUMMARY:
==44813== in use at exit: 72,704 bytes in 1 blocks
==44813== total heap usage: 7 allocs, 6 frees, 73,808 bytes allocated
==44813==
==44813== LEAK SUMMARY:
==44813== definitely lost: 0 bytes in 0 blocks
==44813== indirectly lost: 0 bytes in 0 blocks
==44813== possibly lost: 0 bytes in 0 blocks
==44813== still reachable: 72,704 bytes in 1 blocks
==44813== suppressed: 0 bytes in 0 blocks
==44813== Rerun with --leak-check=full to see details of leaked memory
==44813==
==44813== For counts of detected and suppressed errors, rerun with: -v
==44813== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
If you are still not convinced with the still reachable issue reported in the preceding output, let's try the following code in simple.cpp to understand if this is something in our control:
#include <iostream>
using namespace std;
int main ( ) {
return 0;
}
Execute the following commands:
g++ simple.cpp -std=c++17 -g
valgrind ./a.out --leak-check=full
==62474== Memcheck, a memory error detector
==62474== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==62474== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==62474== Command: ./a.out --leak-check=full
==62474==
==62474==
==62474== HEAP SUMMARY:
==62474== in use at exit: 72,704 bytes in 1 blocks
==62474== total heap usage: 1 allocs, 0 frees, 72,704 bytes allocated
==62474==
==62474== LEAK SUMMARY:
==62474== definitely lost: 0 bytes in 0 blocks
==62474== indirectly lost: 0 bytes in 0 blocks
==62474== possibly lost: 0 bytes in 0 blocks
==62474== still reachable: 72,704 bytes in 1 blocks
==62474== suppressed: 0 bytes in 0 blocks
==62474== Rerun with --leak-check=full to see details of leaked memory
==62474==
==62474== For counts of detected and suppressed errors, rerun with: -v
==62474== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
As you can see, the main() function does nothing but return 0, and Valgrind reports that this program too has the same section: still reachable": 72, 704 bytes in 1 blocks. Hence, what really matters in the Valgrind leak summary is whether there are leaks reported under any or all of the following sections: definitely lost, indirectly lost, and possibly lost.