Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory Leaks in Visual Studio

I have a memory leaks with this simple code using std::thread on Visual Studio Pro 2012:

#include <thread>

void f(){}

int main(){
    std::thread t(f);
    t.join();
    _CrtDumpMemoryLeaks();
    return 0;}

Win32 Output:

Detected memory leaks!
Dumping objects ->
{293} normal block at 0x00A89520, 44 bytes long.
 Data: <                > 01 00 00 00 00 00 00 00 00 00 00 00 0A 00 00 00 
Object dump complete.

x64 Output:

Detected memory leaks!
Dumping objects ->
{293} normal block at 0x00000000003FCB00, 72 bytes long.
 Data: <                > 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
Object dump complete.

If I comment the two first lines of the main method, I have no memory leaks.

Where does it come from ?

EDIT: The leak is still here with that code:

#include <thread>

void f(){}

int main(){
    {
        std::thread t(f);
        t.join();
    }
    _CrtDumpMemoryLeaks();
    return 0;}
like image 977
Arnaud Avatar asked Dec 12 '22 14:12

Arnaud


1 Answers

CrtDumpMemoryLeaks is notoriously unreliable. It may well be that the standard library intentionally leaks a one-time allocation when you first use std::thread. To find out if there's a real memory leak, try this:

for (int i = 0; i < LIMIT; ++i) {
  std::thread t(f); t.join();
}
_CrtDumpMemoryLeaks();

And then see if the leak size increases as you increase LIMIT. If it doesn't, you're fine.

like image 161
Sebastian Redl Avatar answered Dec 14 '22 02:12

Sebastian Redl