析构函数,必须是virtual的,否则会内存泄漏
2012-06-15 19:18
141 查看
AMF0String => AMF0Base
假设析构函数不是virtual:
AMF0String* s = new AMF0String("abc"); delete s; //没有问题,可以调用AMF0String的析构函数释放heap内存。
AMF0String* s = new AMF0String("abc"); delete (AMF0Base*)s; // 内存泄漏,调用的是AMF0Base的析构函数。
看一段代码:
输出是:
[winlin@dev6 temp]$ g++ test.cpp -o test;./test
~MyStringBase
所以子类的析构不会调用。
应该写成:
结果是:
[winlin@dev6 temp]$ g++ test.cpp -o test;./test
~MyStringSub
~MyStringBase
成功调用子类的析构函数。
一个潜在的危险是,在以class定义接口时,必须定义虚析构函数,譬如以下代码可能造成泄漏:
因为它没有定义虚析构函数,当delete (IBytesOutputStream*)os时,子类的析构不会调用。
应该定义为:
举个完整的例子:
可以看到内存一直涨:
假设析构函数不是virtual:
AMF0String* s = new AMF0String("abc"); delete s; //没有问题,可以调用AMF0String的析构函数释放heap内存。
AMF0String* s = new AMF0String("abc"); delete (AMF0Base*)s; // 内存泄漏,调用的是AMF0Base的析构函数。
看一段代码:
#include <stdlib.h> #include <stdio.h> #include <iostream> using namespace std; class MyStringBase { public: MyStringBase(){ } ~MyStringBase(){ cout << "~MyStringBase" << endl; } }; class MyStringSub : public MyStringBase { public: MyStringSub(){ } ~MyStringSub(){ cout << "~MyStringSub" << endl; } }; void MyFunction(MyStringBase* s){ delete s; // s->~xxx(); } int main(){ MyStringSub* str = new MyStringSub(); MyFunction(str); //sleep(100); return 0; }
输出是:
[winlin@dev6 temp]$ g++ test.cpp -o test;./test
~MyStringBase
所以子类的析构不会调用。
应该写成:
#include <stdlib.h> #include <stdio.h> #include <iostream> using namespace std; class MyStringBase { public: MyStringBase(){ } virtual ~MyStringBase(){ cout << "~MyStringBase" << endl; } }; class MyStringSub : public MyStringBase { public: MyStringSub(){ } virtual ~MyStringSub(){ cout << "~MyStringSub" << endl; } }; void MyFunction(MyStringBase* s){ delete s; // s->~xxx(); } int main(){ MyStringSub* str = new MyStringSub(); MyFunction(str); //sleep(100); return 0; }
结果是:
[winlin@dev6 temp]$ g++ test.cpp -o test;./test
~MyStringSub
~MyStringBase
成功调用子类的析构函数。
一个潜在的危险是,在以class定义接口时,必须定义虚析构函数,譬如以下代码可能造成泄漏:
/** * the bytes output stream, which write bytes into. */ class IBytesOutputStream { public: /** * read bytes from stream. */ virtual int32_t Write(int8_t* buffer, u_int32_t size) = 0; };
因为它没有定义虚析构函数,当delete (IBytesOutputStream*)os时,子类的析构不会调用。
应该定义为:
/** * the bytes output stream, which write bytes into. */ class IBytesOutputStream { public: IBytesOutputStream(); virtual ~IBytesOutputStream(); public: /** * read bytes from stream. */ virtual int32_t Write(int8_t* buffer, u_int32_t size) = 0; };
举个完整的例子:
/** g++ -g -O0 use-interface.cpp -o use-interface; ./use-interface */ #include <stdio.h> #include <string.h> #include <unistd.h> class IObject { public: virtual const char* ToString() = 0; }; class String : public IObject { private: char* str; public: String(const char* s){ str = NULL; if(s != NULL){ str = new char[strlen(s) + 1]; strcpy(str, s); } } virtual ~String(){ if(str != NULL){ delete[] str; } } public: virtual const char* ToString(){ return str; } }; void destroy(IObject* obj){ delete obj; } int main(int /*argc*/, char** /*argv*/){ while(true){ destroy(new String("winlin")); usleep(1); } return 0; }
可以看到内存一直涨:
command: ./memview summary 24643 for use-interface VMSIZE: 11784 KB RSS: 856 KB total 712 KB shared 4 KB private clean 140 KB private dirty command: ./memview summary 24643 for use-interface VMSIZE: 11784 KB RSS: 884 KB total 712 KB shared 4 KB private clean 168 KB private dirty command: ./memview summary 24643 for use-interface VMSIZE: 11784 KB RSS: 916 KB total 712 KB shared 4 KB private clean 200 KB private dirty command: ./memview summary 24643 for use-interface VMSIZE: 11784 KB RSS: 948 KB total 712 KB shared 4 KB private clean 232 KB private dirty command: ./memview summary 24643 for use-interface VMSIZE: 11916 KB RSS: 980 KB total 712 KB shared 4 KB private clean 264 KB private dirty command: ./memview summary 24643 for use-interface VMSIZE: 11916 KB RSS: 1012 KB total 712 KB shared 4 KB private clean 296 KB private dirty command: ./memview summary 24643 for use-interface VMSIZE: 11916 KB RSS: 1044 KB total 712 KB shared 4 KB private clean 328 KB private dirty command: ./memview summary 24643 for use-interface VMSIZE: 11916 KB RSS: 1076 KB total 712 KB shared 4 KB private clean 360 KB private dirty command: ./memview summary 24643 for use-interface VMSIZE: 11916 KB RSS: 1104 KB total 712 KB shared 4 KB private clean 388 KB private dirty
相关文章推荐
- 显式调用析构函数发生内存泄漏问题
- C++析构函数必须要用virtual修饰的原因
- 工作总结 mvc外键 public virtual SysUser TransferUser { get; set; } 必须要加 virtual 否则 TransferUser 值为null 还要加[ForeignKey("TransferUser")] Bind 和 ScaffoldColumn(转)
- C++析构函数的自动调用(析构函数必须是虚拟的,这样删除父类指针指向的子类对象,才能同时调用两者的析构函数,否则就没有机会调用子类析构函数)
- 多态基类的析构函数必须加上virtual
- 基类析构函数必须为虚函数否则会造成内存泄漏
- 必须为多态基类声明 virtual 析构函数
- 不要在基类析构函数中调用纯虚函数,否则运行时会报错“pure virtual method called”
- 基类一定要设置虚析构函数,否则会内存泄露
- 内存警告时必须处理否则会闪退
- 2006.9.9 实现定时拷贝屏幕,并保存为指定目录下形成文件DeleteObject(hBmp);//必须要添加,否则会占用内存,越来越小,一定要注意
- 讨论virtual析构函数
- [导入][你必须知道的.NET] 第八回:品味类型---值类型与引用类型(上)-内存有理
- 内存泄漏
- Android 内存泄漏相关
- 在 JNI 编程中避免内存泄漏
- 使用block的时候,导致的内存泄漏
- 程序员必须了解的内存知识
- virtual析构函数
- 必须先判断对象不为空,否则会报空指针错误。