我在 Visual Studio Pro 2012 上使用 std::thread 的这个简单代码有内存泄漏:
#include <thread>
void f(){}
int main(){
std::thread t(f);
t.join();
_CrtDumpMemoryLeaks();
return 0;}
Win32 输出:
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 输出:
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.
如果我注释 main 方法的前两行,我就没有内存泄漏。
它从何而来 ?
编辑:该代码仍然存在泄漏:
#include <thread>
void f(){}
int main(){
{
std::thread t(f);
t.join();
}
_CrtDumpMemoryLeaks();
return 0;}