zoukankan      html  css  js  c++  java
  • Steps To Detect Memory Leak(Draft)

    1. Use _CrtDumpMemoryLeaks() to check whether there is memory leak in program.

    With the help of  _CRTDBG_MAP_ALLOC, it can output memory leak info with file line info for those memory block allocated by malloc(), while it would never print file line info for those allocated by new().

     

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

    Call 

    {

          _CrtDumpMemoryLeaks();

    }

    at the end of the program exit if there is only one exit in the program.

    OR

    Call

    {

         _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

    }

    at the beginning of your program to ensure _CrtDumpMemoryLeaks() would be called when the program exits from any exit point.


    2. Use DEBUG_NEW method to check in detail

    #define DEBUG_NEW new(_NORMAL_BLOCK, THIS_FILE, __LINE__)

    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;

    #endif

    {

         _CrtMemDumpAllObjectsSince(NULL);

    }

    3. Use Tool like LeakFinder (Here: http://www.codeproject.com/KB/applications/leakfinder.aspx)


    I still prefer to use shared_ptr to manage heap allocation instead of putting too much effort on detecting memory leak.

    Keep on learning.


  • 相关阅读:
    Unity模型导入导出
    3D知识补充
    Unity. Navigation和寻路
    Unity坐标系
    Unity材质
    Unity摄像机
    Unity光照
    Unity资源管理与更新
    Unity垃圾回收
    intellij idea 的全局搜索快捷键方法
  • 原文地址:https://www.cnblogs.com/taoxu0903/p/1267432.html
Copyright © 2011-2022 走看看