c/c++语言开发共享使用_CrtDumpMemoryLeaks将数据显示到控制台

我正在使用_CrtDumpMemoryLeaks函数,它工作正常但在文档中承诺不仅要返回true或false,还要打印一些信息。

我试过用:

 _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_DEBUG ); 

但是这里的一些代码没有出现在屏幕上。

 #define _CRTDBG_MAP_ALLOC #include  #include  #include  #include  int main() { slist* students = 0; clist* courses = 0; char c; char buf[100]; int id, num; malloc(100); _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_DEBUG ); printf("there is memmory leaks?: %dn",_CrtDumpMemoryLeaks()); system("pause"); return 0; } 

输出没有关于内存泄漏的数据..为什么会这样?

顺便说一下输出

有memory leaks?:1按任意键继续。 。 。

    如果您在Visual Studio 2010调试实例中运行此操作,则需要查看调试输出(调试 – > Windows – >输出)。

    此外,您不仅需要为错误设置报告模式, 还需要为警告设置报告模式(这将报告内存泄漏):

     _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_DEBUG ); /* Alternatively: * _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE ); * _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDERR ); */ 

    其中为我的程序提供了以下输出:

     Detected memory leaks! Dumping objects -> dump.c(14) : {86} normal block at 0x00834E50, 100 bytes long. Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD Object dump complete. there is memmory leaks?: 1 

    我发现以下代码是最有用的,特别是当您开始将泄漏隔离到特定的方法/函数时:

     // declare memory stare variable _CrtMemState state; ... // create a checkpoint to for current memory state _CrtMemCheckpoint(&state); ... do stuff ... // report differences _CrtMemDumpAllObjectsSince(&state); 

    此例程将转储自检查点以来的所有分配。 IT可以围绕一个函数调用,在启动时和退出时加载等。我也在DllMain进程附加/分离的DLL中使用它。

    _CrtSetReportMode_CrtSetReportFile等结合使用时也很方便

      以上就是c/c++开发分享使用_CrtDumpMemoryLeaks将数据显示到控制台相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/559797.html

      (0)
      上一篇 2021年1月27日
      下一篇 2021年1月27日

      精彩推荐