避免动态分配内存后因异常而导致的内存泄露
来源:互联网 发布:多用户建站系统源码 编辑:程序博客网 时间:2024/05/01 20:35
当在一个函数中动态创建一个对象时如果在delete之前发生异常就会导致内存泄露,原因很简单,因为程序没有处理delete,如果是局部变量,则会保证局部变量的释放.所以我们需要定义一个类来管理NEW来的内存通过在函数中定义局部对象,在异常发生的时候就会自动释放.也就是说自动调用他的析构函数.我们可以在它的构造函数中去NEW一个我们要得对象,而类中保留一个数据成员存放NEW返回的指针,析构时DELETE指针就好了.
#include "iostream"
using std::endl;
using std::cout;
using std::cin;
using std::bad_alloc;
using std::ostream;
class fushu...{
public:
fushu(int x,int y);
friend ostream& operator<<(ostream& out,const fushu& linshi);
fushu operator+(fushu& anotherfu);
~fushu()
...{
cout<<"fushu destroy!"<<endl;
}
int a;
int b;
};
fushu fushu::operator+(fushu& anotherfu)
...{
fushu c(0,0);
if (a==anotherfu.a && b==anotherfu.b) ...{
throw (anotherfu);
}
c.a=a+anotherfu.a;
c.b=b+anotherfu.b;
return c;
}
fushu::fushu(int x,int y): a(x),b(y)
...{
cout<<"fushu constrct!"<<endl;
}
ostream& operator<<(ostream& out,const fushu& linshi)
...{
out<<linshi.a<<" "<<linshi.b<<endl;
return out;
}
class auto_p...{
public:
auto_p(fushu& m);
auto_p(fushu* mm);
~auto_p();
fushu* p;
};
auto_p::auto_p(fushu &m)
...{
p=new fushu(m);
cout<<"auto_p construct!"<<endl;
}
auto_p::auto_p(fushu* mm)
...{
p=mm;
cout<<"auto_p construct with fushu *"<<endl;
}
auto_p::~auto_p()
...{
delete p;
cout<<"auto_p destroy!"<<endl;
}
void fun()
...{
fushu fu(10,20);
auto_p mauto(fu);
auto_p mautoptr(new fushu(10,20));
cout<<(*mautoptr.p+*mauto.p)<<endl;
}
int main()
...{
try...{
fun();
}
catch (fushu & aaa) ...{
cout<<aaa.a<<"and"<<aaa.b<<"same to fushu with mauto point to!"<<endl;
}
return 0;
}
using std::endl;
using std::cout;
using std::cin;
using std::bad_alloc;
using std::ostream;
class fushu...{
public:
fushu(int x,int y);
friend ostream& operator<<(ostream& out,const fushu& linshi);
fushu operator+(fushu& anotherfu);
~fushu()
...{
cout<<"fushu destroy!"<<endl;
}
int a;
int b;
};
fushu fushu::operator+(fushu& anotherfu)
...{
fushu c(0,0);
if (a==anotherfu.a && b==anotherfu.b) ...{
throw (anotherfu);
}
c.a=a+anotherfu.a;
c.b=b+anotherfu.b;
return c;
}
fushu::fushu(int x,int y): a(x),b(y)
...{
cout<<"fushu constrct!"<<endl;
}
ostream& operator<<(ostream& out,const fushu& linshi)
...{
out<<linshi.a<<" "<<linshi.b<<endl;
return out;
}
class auto_p...{
public:
auto_p(fushu& m);
auto_p(fushu* mm);
~auto_p();
fushu* p;
};
auto_p::auto_p(fushu &m)
...{
p=new fushu(m);
cout<<"auto_p construct!"<<endl;
}
auto_p::auto_p(fushu* mm)
...{
p=mm;
cout<<"auto_p construct with fushu *"<<endl;
}
auto_p::~auto_p()
...{
delete p;
cout<<"auto_p destroy!"<<endl;
}
void fun()
...{
fushu fu(10,20);
auto_p mauto(fu);
auto_p mautoptr(new fushu(10,20));
cout<<(*mautoptr.p+*mauto.p)<<endl;
}
int main()
...{
try...{
fun();
}
catch (fushu & aaa) ...{
cout<<aaa.a<<"and"<<aaa.b<<"same to fushu with mauto point to!"<<endl;
}
return 0;
}
这样当异常发生时,会自动调用AUTO_P的析构函数,因为对象是局部的,而析构函数又会DELETE FUSHU的对象,从而达到目的.
- 避免动态分配内存后因异常而导致的内存泄露
- 尽量避免因图片(Bitamp)过大而导致内存溢出 工具类
- 如何避免使用CreateThread函数导致的内存泄露
- 如何避免使用CreateThread函数导致的内存泄露
- 如何避免使用CreateThread函数导致的内存泄露
- 如何避免由于使用Handler导致的内存泄露
- 避免内存泄露的方法
- 避免内存泄露的方法
- 避免内存泄露的方法
- 避免内存泄露的方法
- fork 导致的内存泄露
- 利用WeakHashMap避免因缓存条目过期而造成的内存泄漏问题
- 利用WeakHashMap避免因缓存条目过期而造成的内存泄漏问题
- 内存泄露导致的内存溢出
- 内存泄露导致的内存溢出
- 因WKWebview导致视图控制器释放不掉而内存崩溃的问题
- 因WKWebview导致视图控制器释放不掉而内存崩溃的问题
- 内存异常系列之单例模式导致内存泄露
- 听Steve Yegge讲狼与羊的故事。
- 拖动层效果,兼容IE和FF!
- 在Solaris10 上安装DB2的小问题
- 在Linux 平台上Consle方式安装DB2
- [汇总]Windows Mobile常用程序代码(串口、图象、网络、3D、数据库、音频视频等等)
- 避免动态分配内存后因异常而导致的内存泄露
- Breaking Out of the Browser
- 我快疯掉了
- 心疼女人的12种方式
- 不定期推荐一些好看的小说
- 很久没更新了
- 朴实为美
- 羽毛球杀球小技巧
- 分割字符串 (二)