如何在C ++代码/项目中找到内存泄漏? [英] How to find memory leak in a C++ code/project?

查看:117
本文介绍了如何在C ++代码/项目中找到内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Windows平台上的C ++程序员。我使用Visual Studio 2008。



我通常在代码中出现内存泄漏。



通过检查代码找到内存泄漏,但它是繁琐的,并不总是一个好的方法。



由于我买不起付费内存泄漏检测工具,我希望你们建议最好的方法来避免内存泄漏。


  1. 我想知道程序员如何找到内存泄漏。

  2. 是否有任何标准或过程


  3. 您需要的东西




    • 熟练使用C ++

    • C ++编译器

    • 调试器和其他调查软件工具



    1



    了解操作员基础。 C ++操作符new分配堆内存。 delete操作符释放堆内存。对于每个新,你应该使用删除,以便释放你分配的相同的内存:

      char * str = new char [30]; //分配30个字节来容纳字符串。 

    delete [] str; //清除这30个字节,使str点无处可见。



    2



    已删除。在下面的代码中,str通过第二个分配获取一个新地址。第一个地址不可恢复地丢失,它指向的30个字节也是如此。现在它们是不可能释放的,你有一个内存泄漏:

      char * str = new char [30] //给str一个内存地址。 

    // delete [] str; //删除此行中的第一个注释标记以进行更正。

    str = new char [60]; / *给str另一个内存地址用
    第一个永远消失。* /

    delete [] str; //这将删除60个字节,而不只是第一个30.



    3



    观察这些指针赋值。每个动态变量(堆上分配的内存)需要与一个指针相关联。当动态变量与其指针分离时,就不可能擦除。同样,这会导致内存泄漏:

      char * str1 = new char [30] 

    char * str2 = new char [40];

    strcpy(str1,Memory leak);

    str2 = str1; //坏了!现在40字节是不可能释放。

    delete [] str2; //这将删除30个字节。

    delete [] str1; //可能的访问冲突。这悲剧!



    4



    谨慎使用本地指针。在函数中声明的指针在堆栈上分配,但它指向的动态变量在堆上分配。如果不删除它,它将在程序退出函数后继续:

      void Leak(int x){ 

    char * p = new char [x];

    // delete [] p; //删除要更正的第一个注释标记。

    }



    5


    $ b b

    删除后注意方括号。使用删除自己释放单个对象。使用带方括号的delete[]来释放堆数组。不要这样做:

      char * one = new char; 

    delete [] one; //错误

    char * many = new char [30];

    delete many; //错了!



    6



    - 我通常是与deleaker(请点击这里: http://deleaker.com )。



    谢谢!


    I am a C++ programmer on the Windows platform. I am using Visual Studio 2008.

    I usually end up in the code with memory leaks.

    Normally I find the memory leak by inspecting the code, but it is cumbersome and is not always a good approach.

    Since I can't afford a paid memory leak detection tool, I wanted you guys to suggest the best possible ways to avoid memory leaks.

    1. I want to the know how the programmer can find memory leaks.
    2. Is there any standard or procedure one should follow to ensure there is no memory leak in the program?

    解决方案

    Instructions

    Things You'll Need

    • Proficiency in C++
    • C++ compiler
    • Debugger and other investigative software tools

    1

    Understand the operator basics. The C++ operator "new" allocates heap memory. The "delete" operator frees heap memory. For every "new," you should use a "delete" so that you free the same memory you allocated:

    char* str = new char [30]; // Allocate 30 bytes to house a string.
    
    delete [] str; // Clear those 30 bytes and make str point nowhere.
    

    2

    Reallocate memory only if you've deleted. In the code below, str acquires a new address with the second allocation. The first address is lost irretrievably, and so are the 30 bytes that it pointed to. Now they're impossible to free, and you have a memory leak:

    char* str = new char [30]; // Give str a memory address.
    
    // delete [] str; // Remove the first comment marking in this line to correct.
    
    str = new char [60]; /* Give str another memory address with
                                                        the first one gone forever.*/
    
    delete [] str; // This deletes the 60 bytes, not just the first 30.
    

    3

    Watch those pointer assignments. Every dynamic variable (allocated memory on the heap) needs to be associated with a pointer. When a dynamic variable becomes disassociated from its pointer(s), it becomes impossible to erase. Again, this results in a memory leak:

    char* str1 = new char [30];
    
    char* str2 = new char [40];
    
    strcpy(str1, "Memory leak");
    
    str2 = str1; // Bad! Now the 40 bytes are impossible to free.
    
    delete [] str2; // This deletes the 30 bytes.
    
    delete [] str1; // Possible access violation. What a disaster!
    

    4

    Be careful with local pointers. A pointer you declare in a function is allocated on the stack, but the dynamic variable it points to is allocated on the heap. If you don't delete it, it will persist after the program exits from the function:

    void Leak(int x){
    
    char* p = new char [x];
    
    // delete [] p; // Remove the first comment marking to correct.
    
    }
    

    5

    Pay attention to the square braces after "delete." Use "delete" by itself to free a single object. Use "delete" [] with square brackets to free a heap array. Don't do something like this:

    char* one = new char;
    
    delete [] one; // Wrong
    
    char* many = new char [30];
    
    delete many; // Wrong!
    

    6

    If the leak yet allowed - I'm usually seeking it with deleaker (check it here: http://deleaker.com).

    Thanks!

    这篇关于如何在C ++代码/项目中找到内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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