在专有应用程序的开发过程中。我已经注意到了内存泄漏,到std ::相关字符串在微软的Visual C++ 2013,更新4Visual C++ 2013 std :: string内存泄漏
看看下面的(基本)代码的原型导致内存泄漏:
static std::string MemoryLeakTest()
{
static size_t const test_size = 2002;
std::unique_ptr<char[]> result1(new char[test_size]);
std::string result2(result1.get(), test_size);
return result2;
}
通话它通过:
std::string const testML = MemoryLeakTest();
std::cout << testML << std::endl;
我做错了什么,或者它是在Visual C++ STL内存泄漏?
P.S.这是表示MEM渗透通过VLD检测DebugView中输出:
[11140] WARNING: Visual Leak Detector detected memory leaks!
[11140] ---------- Block 3 at 0x00A95620: 2002 bytes ----------
[11140] Leak Hash: 0x1DA884B6, Count: 1, Total 2002 bytes
[11140] Call Stack (TID 9568):
[11140] 0x0FF5C260 (File and line number not available): MSVCR120D.dll!operator new
[11140] f:\dd\vctools\crt\crtw32\stdcpp\newaop.cpp (6): TestCpp.exe!operator new[] + 0x9 bytes
[11140] c:\work\testcpp\testcpp.cpp (307): TestCpp.exe!MemoryLeakTest + 0xA bytes
[11140] c:\work\testcpp\testcpp.cpp (401): TestCpp.exe!wmain + 0x9 bytes
[11140] f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c (623): TestCpp.exe!__tmainCRTStartup + 0x19 bytes
[11140] f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c (466): TestCpp.exe!wmainCRTStartup
[11140] 0x75557C04 (File and line number not available): KERNEL32.DLL!BaseThreadInitThunk + 0x24 bytes
[11140] 0x77C4B54F (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x8F bytes
[11140] 0x77C4B51A (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x5A bytes
[11140] Data:
你怎么知道它是std :: string导致泄漏? – 2015-03-31 15:03:55
您的代码具有未定义的行为,因为您正在读取未初始化的值。 – 2015-03-31 15:10:15
你是怎么确定这个代码泄漏的? – Casey 2015-03-31 15:58:06