1、背景
C/C++程序开发中,管理堆上分配的内存容易出错,常见的就是内存泄漏。原则上,只要我们足够仔细,在每一次动态分配内存之后,使用完毕后,手动delete即可释放掉内存,可是这必然会加大我们的工作量以及项目的维护难度。
2、RAII机制原理
RAII,即Resource Acquisition Is Initialization,在初始化中获取资源。
RAII机制,通过在栈上创建临时变量,这样临时变量就接管了堆上内存的控制权,当该临时变量声明周期结束时,则对应的堆上内存自然就被释放了。
3、案例:开发者手动管理堆上内存
#include <iostream>
using namespace std;
enum ShapeStyle
{
circle,
triangle,
rectangular
};
class BaseShape
{
public:
virtual void show()=0;
virtual ~BaseShape()
{
}
};
class CircleShape : public BaseShape
{
public:
void show()
{
cout << "I am circle shape." << endl;
}
~CircleShape()
{
cout << "~CircleShape() was called." << endl;
}
};
class TriangleShape : public BaseShape
{
public:
void show()
{
cout << "I am triangle shape." << endl;
}
~TriangleShape()
{
cout << "~TriangleShape() was called." << endl;
}
};
class RectangularShape : public BaseShape
{
public:
void show()
{
cout << "I am rectangular shape." << endl;
}
~RectangularShape()
{
cout << "~RectangularShape() was called." << endl;
}
};
BaseShape *createShape(ShapeStyle style)
{
if(style==circle)
{
return new CircleShape;
}
else if(style==triangle)
{
return new TriangleShape;
}
else if(style==rectangular)
{
return new RectangularShape;
}
else
{
return 0;
}
}
void func()
{
BaseShape *ptr=createShape(circle);
ptr->show();
delete ptr;
ptr=createShape(triangle);
ptr->show();
//delete ptr;//假设这里忘记delete,则会造成内存泄漏
ptr=createShape(rectangular);
ptr->show();
delete ptr;
}
int main(int argc, char *argv[])
{
func();
return 1;
}
输出:
?I am circle shape. ~CircleShape() was called. I am triangle shape. I am rectangular shape. ~RectangularShape() was called.
可以看出,~TriangleShape()这个析构函数没有被调用,也就是内存没有被释放,造成了内存的浪费。
4、案例:使用RAII思想管理动态内存
#include <iostream>
using namespace std;
enum ShapeStyle
{
circle,
triangle,
rectangular
};
class BaseShape
{
public:
virtual void show()=0;
virtual ~BaseShape()
{
}
};
class BaseShapeWrapper
{
public:
//构造函数中获取资源
explicit BaseShapeWrapper(BaseShape *ptr=0) : d(ptr)
{
}
//析构函数中释放资源
~BaseShapeWrapper()
{
if(d)
{
delete d;
d=0;
}
}
BaseShape *operator->() const
{
return d;
}
void reset(BaseShape *other)
{
if(d==other)
{
return;
}
BaseShape *oldD = d;
d = other;
delete oldD;
}
private:
BaseShape *d;
};
class CircleShape : public BaseShape
{
public:
void show()
{
cout << "I am circle shape." << endl;
}
~CircleShape()
{
cout << "~CircleShape() was called." << endl;
}
};
class TriangleShape : public BaseShape
{
public:
void show()
{
cout << "I am triangle shape." << endl;
}
~TriangleShape()
{
cout << "~TriangleShape() was called." << endl;
}
};
class RectangularShape : public BaseShape
{
public:
void show()
{
cout << "I am rectangular shape." << endl;
}
~RectangularShape()
{
cout << "~RectangularShape() was called." << endl;
}
};
BaseShape *createShape(ShapeStyle style)
{
if(style==circle)
{
return new CircleShape;
}
else if(style==triangle)
{
return new TriangleShape;
}
else if(style==rectangular)
{
return new RectangularShape;
}
else
{
return 0;
}
}
void func()
{
//局部变量
BaseShapeWrapper ptr(createShape(circle));
ptr->show();
ptr.reset(createShape(triangle));
ptr->show();
ptr.reset(createShape(rectangular));
ptr->show();
}
int main(int argc, char *argv[])
{
func();
return 1;
}
运行:
I am circle shape. ~CircleShape() was called. I am triangle shape. ~TriangleShape() was called. I am rectangular shape. ~RectangularShape() was called.
从运行结果可以看出,堆上创建的三个对象都被释放了,对于调用者来说,确没有执行过一个delete操作,显然这种方式很好,大大提高了工作效率,同时,代码也更加健壮。 ?
|