More Effective C++条款9:利用destructors避免泄露资源

1C++为什么要引入exception?

因为exception无法被忽略。如果一个函数利用“设定状态变量”的方式或是利用“返回错误码”的方式发出一个异常信号,无法保证此函数的调用者会检查那个变量或检验那个错误码(特别是当需要判断返回值的地方特别多的时候,很难每一次调用都去判断一下返回值,并且如果每次调用都去判断返回值的话,会让代码显得啰嗦)。于是程序的执行可能会一直继续下去,远离错误发生地点。但是如果函数以抛出exception的方式发出异常信号,而该exception未被捕捉,程序的执行便会立该中止。

2 利用destructor避免泄露资源

举例:你正在为“小动物收养保护中心”“编写一个软件,收养中心每天都会产生一个文件,其中有它所的当天收养个案。你的工作就是写一个程序,读这些文 件,然后为每一个收养个案做适当处理。合理的想法是定义一个抽象基类ALA(Adorable Little Animal), 再从中派生出针对小狗和小猫的具体类。其中有个虚函数processAdoption, 负责”因动物类而异“的必要处理动作。

你需要一个函数,读取文件内容,并视文件内容产生一个Puppy object或一个Kitten object.

代码如下:

#include <iostream>
#include <string.h>
#include <fstream>
#include <memory>
using namespace std;

class ALA
{
public:
    virtual void processAdoption() = 0;
    virtual ~ALA() { printf("ALA::%s\r\n",__func__); }
};

class Puppy: public ALA
{
public:
   void processAdoption() 
   {  
      printf("Puppy::%s\r\n",__func__); 
      throw invalid_argument("throw execption for testing!");
   }

    ~Puppy() { printf("Puppy::%s\r\n",__func__); }
};

class Kitten: public ALA
{
public:
    void processAdoption() {  printf("Kitten::%s\r\n",__func__); }
    ~Kitten() { printf("Kitten::%s\r\n",__func__); }
};

ALA * readALA(istream& dataSource)
{
    char name[10] = {0};
    dataSource.getline(name, sizeof(name));

    if(strcmp(name, "puppy") == 0)
    {
        printf("new a puppy\r\n");
        return new Puppy();  
    }
    
    printf("new a kitten\r\n");
    return new Kitten();  
}

void processAdoptions()
{
    filebuf buf;
    if (buf.open("/root/Coding/more_effective_c++_item9/animal", ios::in) == nullptr)
    {
        printf("openfile failed\r\n");
	return;
    }

    istream is(&buf);
    while(is)
    {
        ALA *pa = readALA(is);
        pa->processAdoption();
        delete pa;
    }
}

int main()
{
    try 
    {
        processAdoptions();
    }
    catch (invalid_argument e)
    {
        printf("%s\r\n", e.what());
        return -1;
    }

    return 0;
}

这个文件/root/Coding/more_effective_c++_item9/animal里面的内容如下:

puppy
kitten

当Puppy::processAdoption抛出异常时,pa对象没有被delete, 从而造成了资源泄露。那有没有办法在抛出异常时也将资源做正常释放。

方法1:catch processAdoption exception,在异常发生的时候手动释放资源,但是这样会导致代码比较繁琐,不那么clean.

void processAdoptions()
{
    filebuf buf;
    if (buf.open("/root/Coding/more_effective_c++_item9/animal", ios::in) == nullptr)
    {
        printf("openfile failed\r\n");
	return;
    }

    istream is(&buf);
    while(is)
    {
        ALA *pa = readALA(is);
		try
		{
            pa->processAdoption();
            
		}
		catch(...)
		{
			delete pa;			
		    throw;
		}
		
		delete pa;
    }
}

方法2:将释放资源的操作封装在析构函数里面,这样当局部变量生命周期到时,其会调用析构函数(不管以什么样的方式释放局部变量,正常退出或者抛exception退出)。C++提供的智能指针正是可以用来满足这样的需求。将 processAdoptions稍微改写一下:

void processAdoptions()
{
    filebuf buf;
    if (buf.open("/root/Coding/more_effective_c++_item9/animal", ios::in) == nullptr)
    {
        printf("openfile failed\r\n");
	    return;
    }

    istream is(&buf);
    while(is)
    {
        unique_ptr<ALA> pa(readALA(is));
        pa->processAdoption();
    }
}

程序的输出如下:

./a.out

new a puppy
Puppy::processAdoption
Puppy::~Puppy
ALA::~ALA
throw execption for testing!

可以看到,当抛出异常时,资源也得到正确释放了,并且代码实现也比较简洁。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一条叫做nemo的鱼

你的鼓励是我最大的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值