检测基本内存泄漏情况的代码 [英] Code to detect basic memory leak scenarios

查看:78
本文介绍了检测基本内存泄漏情况的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了检测以下三个条件的代码:
1.使用新的而不删除.
2.使用"new int;"一种声明...换句话说,地址未占用已分配的内存.
3. int * p = new int [10];
删除[] p;
谁能帮助我,并为c ++中的内存分配建议各种可能的语法,以便使我的代码更健壮.

I have written a code to detect the following three conditions :
1. use of new without delete.
2. using "new int;" kind of declaration...in other words address not taken of memory allocated.
3. int*p = new int [10];
delete []p;
Can anyone help me and suggest various possible syntax for memory allocations in c++ so that i can make my code more robust.

推荐答案

1.使用新的而不删除
打算这样做的一些方案:

  • 函数返回指向已分配内存的指针.调用函数必须释放内存.
  • 将指针分配给参数的函数(通过指针或引用).调用函数必须释放内存.
  • 将指针分配给对象参数(类/结构成员)的函数.调用函数必须释放内存.
  • 指针存储在类成员中并在其他地方释放(例如在析构函数中).在这种情况下,delete指令可能在另一个源文件中.
1. Use of new without delete
Some scenarios where this is intended:

  • Functions returning pointer to allocated memory. Calling function must free the memory.
  • Functions assigning pointer to parameter (by pointer or reference). Calling function must free the memory.
  • Functions assigning pointer to object parameter (class/struct member). Calling function must free the memory.
  • Pointer is stored in class member and freed somewhere else (e.g. in destructor). The delete instruction may be in another source file in this case.
CMyObject o2 = new CMyObject();
CMyObject o2 = new CMyObject(p1, p2);



其他内存分配功能是:


  • malloc,请参阅《运行时库参考》,以了解有关调用malloc的其他CRT函数的信息.其中有些需要释放分配的内存(例如_getcwd).
  • calloc

  • LocalAlloc
  • GlobalAlloc,通常会在其他地方释放代码.
  • HeapAlloc
  • VirtualAlloc
  • 各种Windows SDK函数可分配内存.


  • Other memory allocation functions are:


    • malloc, see the Run-Time Library Reference about other CRT functions that call malloc. Some of them require freeing allocated memory (e.g. _getcwd).
    • calloc

    • LocalAlloc
    • GlobalAlloc, code is often freed elsewhere.
    • HeapAlloc
    • VirtualAlloc
    • Various Windows SDK functions that allocate memory.

    • 问题不在于查找new和Delete的出现,而是找出其中的哪些是配对的. .破坏可能不会在与构造相同的函数中发生,因此,如果您要做的只是在函数中检查新值而不删除,那么您肯定会发现很多误报.

      另外,您还应该特别注意placement new运算符,乍一看,它看起来像是一个内存分配,但实际上根本没有分配.

      就是说,有很多工具已经在进行这种静态分析.为什么您不只是搜索一下-我相信您也可以找到一些开源项目.
      The problem isn''t so much finding occurences of new and delete, but finding out which of those are paired. Destruction may not happen in the same function as construction, so you''re bound to find many false positives if all you do is check for new without delete within a function.

      Also you should take special care of the placement new operator, which on first sight looks like a memory allocation, but in truth does no allocation at all.

      That said, there are plenty of tools out there that already do such static analysis. Why don''t you just search a bit - I''m sure you can find some open source projects too.


      别忘了malloc/realloc/callocfree.

      不要忘记多态性.
      不要忘记指针的所有权(T * p =新T; T * p1 = p;删除p1;)内/外函数.


      我发现了这个 http://suif.stanford.edu/~dlheine/thesis.pdf [ ^ ]尽管颇具学术性,但仍令人困惑.
      Don''t forget malloc/realloc/calloc and free.

      Don''t forget polymorphism.
      Don''t forget pointer ownership ( T* p = new T;T* p1 = p; delete p1; ) either intra/extra functions.


      I found this http://suif.stanford.edu/~dlheine/thesis.pdf[^] that is interseting albeit quite academic.


      这篇关于检测基本内存泄漏情况的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆