delete与double free
在看一些C++的文章的时候,经常会看到关于delete与free的区别,delete不需要检查指针是否为NULL。如下面一篇文章http://www.cnblogs.com/zhuyp1015/archive/2012/07/20/2601698.html。
但是有时候,又会看到由于delete引起的double free or corruption,我们可以写一个很简单的程序,来测试delete会不会引起double free。
#include <iostream> using namespace std; #ifndef NULL #define NULL 0 #endif class A { public: A(){ cout << "\tA was constructed" << endl; } ~A(){ cout << "\tA was deconstructed" << endl; } }; int main() { int* a = NULL; cout << "befor delete: " << a << endl; delete a; cout << "delete once: " << a << endl; delete a; cout << "delete twice: " << a << endl; /* a = new int(3); cout << "after new an integer: " << a << endl; delete a; cout << "delete once: " << a << endl; delete a; cout << "delete twice: " << a << endl; */ /* A* b = new A(); cout << "after new A: " << b << endl; delete b; cout << "delete once: " << b << endl; delete b; cout << "delete twice: " << b << endl; */ int* c = new int[3]; cout << "after new an array: " << c << endl; delete [] c; cout << "delete once: " << c << endl; delete [] c; cout << "delete once: " << c << endl; return 0; }
下面是结果,
测试环境,ubuntu10.04,g++4.4.3.
出现double free了。从输出可以看到,如果指针ptr的值不为NULL,那么在第一次delete之后,ptr也不会被置为0,然后进行第二次delete的时候,就出现double free。
上面代码里有一段是创建一个类的实例,在构造函数和析构函数中有输出,测试的时候,在第二次delete的时候,析构函数又执行了一次,然后就出现double free了。
如何避免double free呢?
一种方法就是在delete之后将指针置为NULL。有其它方法吗?检查指针是否为0?这个。。。