zoukankan      html  css  js  c++  java
  • visual studio如何检查内存泄露?

    Visual Studio有专门的插件叫做Visual Leak Detector (VLD)
    Visual Leak Detector for Visual C++ 2008/2010/2012/2013
    安装之后,他会给你加入几个头文件和lib,原理可以参考这里
    Visual Leak Detector
    使用方法非常简单,只需要在你的检测cpp加上这样一句话
    #include <vld.h>
    然后使用debug模式运行,如果有内存泄露,会在Output窗口打出详细的泄露的堆栈,双击堆栈就可以定位到自己的代码(我故意加把一个delete注释掉得到如下截图)
     
    ==========================================================
    clang有一个编译选项,不知道哪个版本才开始有的。
    -fsanitize=address
    然后对于下面这段代码,
    int main() {
        int *p = new int(100);
    
        return 0;
    }
    
    运行会直接出错.
    =================================================================
    ==18414==ERROR: LeakSanitizer: detected memory leaks

    Direct leak of 4 byte(s) in 1 object(s) allocated from:
    #0 0x43345b in operator new(unsigned long) (/tmp/a.out+0x43345b)
    #1 0x4b8569 in main (/tmp/a.out+0x4b8569)
    #2 0x7f909df7803f in __libc_start_main (/usr/lib/libc.so.6+0x2003f)

    SUMMARY: AddressSanitizer: 4 byte(s) leaked in 1 allocation(s).

    作者:知乎用户
    链接:https://www.zhihu.com/question/26729933/answer/33848182
    来源:知乎
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。




  • 相关阅读:
    mysql主从复制
    gitlab安装
    nginx新加模块编译
    flask编写prometheus采集指标脚本
    powerdns的安装
    grafana中prometheus的查询语句
    python编写prometheus的监控指标
    maven常用命令参数
    flask架构中的方法学习
    Java命名规范
  • 原文地址:https://www.cnblogs.com/bruce1992/p/14010884.html
Copyright © 2011-2022 走看看