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

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

问题描述

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

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. 我想知道程序员如何发现内存泄漏.
  2. 是否应该遵循任何标准或程序来确保程序中没有内存泄漏?

推荐答案

说明

你需要的东西

  • 精通 C++
  • C++ 编译器
  • 调试器和其他调查软件工具

了解操作员基础知识.C++ 运算符 new 分配堆内存.delete 操作符释放堆内存.对于每个 new,您应该使用 delete 以便释放您分配的相同内存:

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

仅当您已删除时才重新分配内存.在下面的代码中,str 通过第二次分配获得一个新地址.第一个地址不可挽回地丢失了,它指向的 30 个字节也是如此.现在它们不可能被释放,而且你有内存泄漏:

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

注意那些指针分配.每个动态变量(堆上分配的内存)都需要与一个指针相关联.当动态变量与其指针脱离关联时,将无法擦除.同样,这会导致内存泄漏:

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

小心本地指针.您在函数中声明的指针分配在堆栈上,但它指向的动态变量分配在堆上.如果你不删除它,它会在程序退出函数后持续存在:

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

注意删除"后的方括号.单独使用 delete 来释放单个对象.使用带有方括号的 delete [] 来释放堆数组.不要做这样的事情:

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

如果泄漏尚未允许 - 我通常会通过 deleaaker 寻找它(在此处查看:http://deleaker.com).

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

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