1. boost::any 保存原始指针
int* p = new int(10);
// 应该用 shared_ptr<int> p(new int(10));
boost::any a = p;
// 危险,会造成内存泄露
any 的析构函数会删除内部的 holder 对象(any 是包装类),如果类型是指针,any 并不会对指针执行 delete 操作,因此,如果用 any 保存原始指针(raw pointer)会造成内存泄露,替代方案是使用智能指针来存入 any 容器。
2. 单例类
class Singleton
{
public:
static Singleton* instance()
{
if (!_instance)
_instance = new Singleton;
// 危险,会造成内存泄露,应使用智能指针对原始指针封装
return _instance;
}
private:
Singleton() {}
static Singleton* _instance;
};