這段程序的內存泄漏在哪?

按照輪子哥博客里的方法試了下,不知道問題出在哪?

#include&
#include&
using namespace std;
int main(){
int ia[] = {0, 1, 2, 3, 4};
unsigned int i;
vector& iv(ia, ia + 5);
for (auto v : iv)
cout &<&< v &<&< " "; cout &<&< endl; _CrtDumpMemoryLeaks(); return 0; }

vs2013輸出如下:

Detected memory leaks!

Dumping objects -&>

{142} normal block at 0x00DE9720, 20 bytes long.

Data: &< &> 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00

{141} normal block at 0x00DE4D90, 8 bytes long.

Data: & 4C F7 B1 00 00 00 00 00

Object dump complete.


因為你的vector還沒釋放啊。所以你要這麼寫:

int main()
{
{
//Fuck everything here
}
_CrtDumpMemoryLeaks();
return 0;
}

話說如果你加入這些的話(要確保每一個cpp文件都能看見)

#define _CRTDBG_MAP_ALLOC
#include &
#include &
#define VCZH_CHECK_MEMORY_LEAKS_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new VCZH_CHECK_MEMORY_LEAKS_NEW

那列印memory leak的時候,Visual Studio還能在output窗口裡面顯示出這段內存是在哪裡創建出來的,雙擊可以跳過去。


main的塊還沒結束,堆裡面的vector還沒釋放,就這樣


atexit - C++ Reference

#include&
#include&
#include &
using namespace std;
void fnExit1(void)
{
_CrtDumpMemoryLeaks();
}
int main(){
atexit(fnExit1);
// int* pInt = new int; // This leaks.
int ia[] = { 0, 1, 2, 3, 4 };
unsigned int i;
vector& iv(ia, ia + 5);
for (auto v : iv)
cout &<&< v &<&< " "; cout &<&< endl; return 0; }


貌似是輸出的是你堆棧上的變數,這個當main函數執行完了才會釋放的。或者可以向輪子哥說的那樣用大括弧括起來


沒泄露啊!!!1


你都沒有new對象,所以的變數都在棧上。怎麼泄露?


推薦閱讀:

如何單獨提取出MSVC2017編譯器並在其他IDE使用?
怎麼評價Visual Studio Community 2013?
VS2013解決方案資源管理器里什麼都不顯示,如何解決?

TAG:C | MicrosoftVisualStudio |