How to remove memory leak from std::list
?
This is just example code :
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include <iostream>
#include <list>
using namespace std;
void main()
{
list<int> a;
a.clear();
_CrtDumpMemoryLeaks();
}
When I try to run it, it shows some memory leak.
So, how to remove it?
There is likely no memory leak. What the report is telling you is that the memory has not yet been deallocated, which is true. It will be deallocated at the end of the current scope - after the _CrtDumpMemoryLeaks()
has run.
Alter the code as follows; it will provide you with a more accurate answer:
void main()
{
{
list<int> a;
a.clear();
}
_CrtDumpMemoryLeaks();
}
Note the movement of the a
container to its own scope.