使用 mingw-64 的 C++ VScode 中的内存泄漏,CRT:_CrtDumpMemoryLeaks() 未显示内存泄漏

问题描述 投票:0回答:1

我在 Windows 11 上使用 VScode 通过 ming-w64 运行 C++ 代码。 我已经按照所有这些步骤

安装了ming-w64

我正在运行一个存在故意内存泄漏的代码,以查看是否有 _CrtDumpMemoryLeaks() 的报告。

这是我的代码:

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

#include <iostream>
#ifdef _DEBUG
    #define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
    // Replace _NORMAL_BLOCK with _CLIENT_BLOCK if you want the
    // allocations to be of _CLIENT_BLOCK type
#else
    #define DBG_NEW new
#endif

using namespace std;

int main()
{
    _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
    _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_DEBUG );
   _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDOUT );
   _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE );
   _CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDOUT );
   _CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE );
   _CrtSetReportFile( _CRT_ASSERT, _CRTDBG_FILE_STDOUT );
   
    int *i2 = new int(2);
    cout << "address of i2 :" << i2 << endl;
    bool check = _CrtDumpMemoryLeaks();
    cout << "MEMORY check :" << check << endl;
    cout << "After CrtDumpMemoryLeaks" << endl;
}

这是终端:

PS C:\Users\שראל\Desktop\C_proj\projects\helloworld>  & 'c:\Users\שראל\.vscode\extensions\ms-vscode.cpptools-1.9.7\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-o0zd5lqu.up2' '--stdout=Microsoft-MIEngine-Out-gva0ndx0.ycm' '--stderr=Microsoft-MIEngine-Error-eec2m2xg.4x5' '--pid=Microsoft-MIEngine-Pid-fakhxofs.oyp' '--dbgExe=C:\msys64\mingw64\bin\gdb.exe' '--interpreter=mi'
address of i2 :0x190a9ff1a90
MEMORY check :0
After CrtDumpMemoryLeaks

输出为空。 我不明白我在这里做错了什么。

visual-studio-code memory-leaks mingw-w64 crtdbg.h
1个回答
0
投票

MinGW 显然不支持开箱即用的 crtdbg 功能,研究一下 mingw64\includ

© www.soinside.com 2019 - 2024. All rights reserved.