析构函数,必须是virtual的,否则会内存泄漏

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

winlinvip

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值