C++编程思想 第2卷 第1章 异常处理 清理 使所有事物都成为对象

为了防止资源泄漏,两种方式来防止 不成熟的 资源分配方式

在构造函数中捕获异常,用于释放资源
在对象的构造函数中分配资源,并且在对象的析构函数中释放资源

使用方法可以使对象的每一次资源分配都具有原子性,由于资源分配成为
局部对象生命周期的一部分,如果某次分配失败了,那么在栈反解的时候,
其他已经获得所需资源的对象能够被恰当地清理

资源获得式初始化 Resource Acquisition Is Initialization RAII,使得
对象对资源控制的时间与对象的 生命周期相等。

//: C01:Wrapped.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// Safe, atomic pointers.
#include <iostream>
#include <cstddef>
using namespace std;

// Simplified. Yours may have other arguments.
template<class T, int sz = 1> class PWrap {
  T* ptr;
public:
  class RangeError {}; // Exception class
  PWrap() {
    ptr = new T[sz];
    cout << "PWrap constructor" << endl;
  }
  ~PWrap() {
    delete[] ptr;
    cout << "PWrap destructor" << endl;
  }
  T& operator[](int i) throw(RangeError) {
    if(i >= 0 && i < sz) return ptr[i];
    throw RangeError();
  }
};

class Cat {
public:
  Cat() { cout << "Cat()" << endl; }
  ~Cat() { cout << "~Cat()" << endl; }
  void g() {}
};

class Dog {
public:
  void* operator new[](size_t) {
    cout << "Allocating a Dog" << endl;
    throw 47;
  }
  void operator delete[](void* p) {
    cout << "Deallocating a Dog" << endl;
    ::operator delete[](p);
  }
};

class UseResources {
  PWrap<Cat, 3> cats;
  PWrap<Dog> dog;
public:
  UseResources() { cout << "UseResources()" << endl; }
  ~UseResources() { cout << "~UseResources()" << endl; }
  void f() { cats[1].g(); }
};

int main() {
  try {
    UseResources ur;
  } catch(int) {
    cout << "inside handler" << endl;
  } catch(...) {
    cout << "inside catch(...)" << endl;
  }
  getchar();
} ///:~

使用模板来封装指针的方法,这个方法使得每个指针都被嵌入到对象中

PWrap模板是典型的使用异常的例子:在operator[]中使用了一个称作Range
Error的嵌套类,如果参数越界,则创建一个 RangeError类型的异常对象

输出
Cat()
Cat()
Cat()
PWrap constructor
Allocating a Dog
~Cat()
~Cat()
~Cat()
PWrap destructor
inside handler

程序为Dog分配存储空间的时间再一次抛出了异常,但是这一次Cat数组中
的对象被恰当的清理了,没有出现内存泄漏

猜你喜欢

转载自blog.csdn.net/eyetired/article/details/81570070