1. 程式人生 > 其它 >vs下檢測程式是否有記憶體洩露

vs下檢測程式是否有記憶體洩露

技術標籤:c++c++記憶體洩漏

新增標頭檔案和巨集定義

#include <crtdbg.h>

#ifdef _DEBUG
#define new new (_NORMAL_BLOCK, __FILE__, __LINE__)
#endif

在main函式最後新增

_CrtDumpMemoryLeaks();

沒有記憶體洩露的測試程式碼和除錯資訊

#include <crtdbg.h>
#include <iostream>

#ifdef _DEBUG
#define new new (_NORMAL_BLOCK, __FILE__, __LINE__)
#endif int main() { int* num = new int(10); delete num; _CrtDumpMemoryLeaks(); }

在這裡插入圖片描述
存在記憶體洩露的測試程式碼和除錯資訊

#include <crtdbg.h>
#include <iostream>

#ifdef _DEBUG
#define new new (_NORMAL_BLOCK, __FILE__, __LINE__)
#endif

int main()
{
    int* num = new int(10);
    //delete num;
    _CrtDumpMemoryLeaks
(); }

在這裡插入圖片描述