zoukankan      html  css  js  c++  java
  • Visual Studio 内存泄漏检测方法

    Visual Studio 内存泄漏检测方法
    非MFC程序可以用以下方法检测内存泄露:
    1.程序开始包含如下定义:
    #ifdef _DEBUG
    #define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
    #else
    #define DEBUG_CLIENTBLOCK
    #endif // _DEBUG
    #define _CRTDBG_MAP_ALLOC
    #include <stdlib.h>
    #include <crtdbg.h>
    #ifdef _DEBUG
    #define new DEBUG_CLIENTBLOCK
    #endif // _DEBUG
    2.程序中添加下面的函数:
    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF);
    Debug版本程序运行结束后如有内存泄漏,输出窗口中会显示类似信息:
    Detected memory leaks!
    Dumping objects ->
    g:programs est est.cpp(16) : {51} client block at 0x00385C58, subtype 0, 4 bytes long.
    Data: <    > CD CD CD CD 
    Object dump complete.
    MFC程序内存泄漏检测方法:
    1.在 CMyApp 中添加如下三个 CMemoryState 类的成员变量:
    #ifdef _DEBUG
    protected:
          CMemoryState m_msOld, m_msNew, m_msDiff;
    #endif // _DEBUG
    2.在 CMyApp::InitInstance() 中添加如下代码:
    #ifdef _DEBUG
          m_msOld.Checkpoint();
    #endif // _DEBUG
    3.在 CMyApp::ExitInstance() 中添加如下代码:
    #ifdef _DEBUG
          m_msNew.Checkpoint();
          if (m_msDiff.Difference(m_msOld, m_msNew))
          {
                afxDump<<" Memory Leaked : ";
                m_msDiff.DumpStatistics();
                afxDump<<"Dump Complete ! ";
          }
    #endif // _DEBUG
    Debug版本程序运行结束后如有内存泄漏,输出窗口中会显示类似信息:
    Memory Leaked :
    0 bytes in 0 Free Blocks.
    8 bytes in 1 Normal Blocks.
    0 bytes in 0 CRT Blocks.
    0 bytes in 0 Ignore Blocks.
    0 bytes in 0 Client Blocks.
    Largest number used: 8825 bytes.
    Total allocations: 47506 bytes.
    Dump Complete !
    Detected memory leaks!
    Dumping objects ->
    g:programschatchatdlg.cpp(120) : {118} normal block at 0x00D98150, 8 bytes long.
    Data: <        > A8 7F D9 00 01 00 00 00 
    Object dump complete.
  • 相关阅读:
    实验一报告 20135238&20135207
    第十周
    极客Web前端开发资源大荟萃#022
    一个不错的编程小挑战 没事的时候可以试试
    变形金刚的能量方块(含代码)
    Angular控制器之间的数据通信
    使用HTML5本地 Drag和Drop API(native API)
    用requestAnimationFrame优化你的javascript动画
    模板字符串
    ES6的全新特性:模板字符串
  • 原文地址:https://www.cnblogs.com/cplover/p/3372038.html
Copyright © 2011-2022 走看看