在專有應用程序的開發過程中。我已經注意到了內存泄漏,到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