您的位置:首页 > Web前端

析构函数,必须是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的析构函数。

看一段代码:

#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息