怎么有效的防止内存泄漏

http://blog.csdn.net/couhujia/article/details/8474905

C++中如何防止内存泄漏(来自百度搜索)

1.尽量不去手动分配内存。比如,我一般不使用数组,而使用STL的vector.
2.如果需要手动分配数组,尽量使用STL中的分配方式,或者使用STL和BOOST中的智能指针。
http://www.cppblog.com/wanghaiguang/archive/2013/05/02/199909.aspx
2. 对于C和C++这种没有Garbage Collection 的语言来讲,我们主要关注两种类型的内存泄漏:

   堆内存泄漏(Heap leak)。对内存指的是程序运行中根据需要分配通过malloc,realloc new等从堆中分配的一块内存,再是完成后必须通过调用对应的 free或者delete 删掉。如果程序的设计的错误导致这部分内存没有被释放,那么此后这块内存将不会被使用,就会产生Heap Leak. 

  系统资源泄露(Resource Leak).主要指程序使用系统分配的资源比如 Bitmap,handle ,SOCKET等没有使用相应的函数释放掉,导致系统资源的浪费,严重可导致系统效能降低,系统运行不稳定。 

首先说说标题可能取得有些大,但是可以理解为编程过程中有效的防止写的代码中有内存泄漏。好了废话不多说了,首先看下面一段代码。

[cpp]  view plain copy
  1. class Image  
  2. {  
  3.     public:  
  4.         Image(const std::string& imgFileName);  
  5.         ...  
  6. }  
  7.   
  8. class Voice  
  9. {  
  10.     public:  
  11.         Voice(const std::string& vFileName);  
  12.         ...  
  13. }  
  14.   
  15. class People  
  16. {  
  17.     public:  
  18.             People(const std::string& n,const int& a,const int& h,const std::stirng& imgFileName,const std::string& vFileName);  
  19.             ~People( );  
  20.     private:  
  21.             string name;  
  22.             int       age;  
  23.             int       height;  
  24.             Image *pImg;          //图像  
  25.             Voice   *pVoi;          //声音  
  26. };  
  27.   
  28. People::People( const std::string& n,const int& a,const int& h,const std::stirng& imgFileName,const std::string& vFileName )  
  29. :name(n),age(a),height(h),pImg(0),pVoi(0)  
  30. {  
  31.         if(imgFileName != "")  
  32.         {  
  33.             pImg = new Image( imgFileName ) ;  
  34.         }  
  35.         if(vFileName != " ")  
  36.         {  
  37.             pVoi = new Voice ( vFileName );  
  38.         }  
  39. }  
  40.   
  41. People::~People( )  
  42. {  
  43.         delete pImg;  
  44.         delete pVoi;  
  45. }  
上面代码粗略看似没有问题,但是有没有想到如果People构造函数出错(内存不足,无法分配内存)怎么办?其结果可以预见,就是一个异常抛出来。但是我们仔细想想此时如果已经构造了Image类对象,而在构造Voice类对象时抛出的错误,这个情况会怎么办?程序会因为异常而停止,后面代码不会执行,那么pImg指针所指向的内存就不会得到正确的释放,那么内存就泄漏了。情况如下面代码:
[cpp]  view plain copy
  1. ...  
  2. People *p =NULL;  
  3. try{  
  4.     p=new People("test",20,170,".../images/image01.jpg","../voices/voice01.dat");  
  5.     ...  
  6. catch( ... ){  
  7.     delete p;  
  8.     throw;  
  9.  }  
  10.  delete p;  
  11. }  
仔细想想 new People("test",20,170,".../images/image01.jpg","../voices/voice01.dat")里,如果最后为Image分配的内存被丢失,因为new操作没有成功完成,程序不会p进行赋值操作。所以catch中是没有任何操作的,已被分配的内存就丢失了。
因为对象在构造中抛出异常后C++不负责清除对象,所以我们需要重新设计构造函数让它们在运到异常的时候自己能清除所占用的内存。
[cpp]  view plain copy
  1. People::People( const std::string& n,const int& a,const int& h,const std::stirng& imgFileName,const std::string& vFileName )  
  2. :name(n),age(a),height(h),pImg(0),pVoi(0)  
  3. {  
  4.     try {  
  5.         if(imgFileName != "")  
  6.         {  
  7.             pImg = new Image( imgFileName ) ;  
  8.         }  
  9.         if(vFileName != " ")  
  10.         {  
  11.             pVoi = new Voice ( vFileName );  
  12.         }  
  13.     }  
  14.     catch( ...) {  
  15.         delete pImg ;  
  16.         delete pVoi;  
  17.         throw ;  
  18. }  
这样就行了,解决上面的情况。让成员变量成为const指针,这样设计也合理,避免指针无意被改变。
[cpp]  view plain copy
  1. Image * const pImg;          //图像  
  2. Voice   * const pVoi;        //声音  
那么这样就只能用成员初始化列表为指针初始化,就没有其他地方可以给const指针赋值了。
[cpp]  view plain copy
  1. People::People( const std::string& n,const int& a,const int& h,const std::stirng& imgFileName,const std::string& vFileName )  
  2. :name(n),age(a),height(h),  
  3. pImg( imgFileName !="" ? new Image( imgFileName ) : 0 ),  
  4. pVoi( vFileName != "" ? new Voice( vFileName ) : 0)  
  5. {}  
如果这样就重新回到上面所遇到的问题,即构造过程中抛出异常,指针可能无法正确的释放所占内存。那么我们可以进一步对代码进行改进,如下:
[cpp]  view plain copy
  1. People::People( const std::string& n,const int& a,const int& h,const std::stirng& imgFileName,const std::string& vFileName )  
  2. :name(n),age(a),height(h),  
  3. pImg( initImage( imgFileName ) ),  
  4. pVoi(  initVoice( vFileName ) )  
  5. {}  
  6.   
  7. Image* People::initImage(const string& imgFileName)   
  8. {  
  9.     if(imgFileName !=""return new Image(imgFileName);  
  10.     else return 0;  
  11. }  
  12.   
  13. Voice* People::initVoice(const string& vFileName)  
  14. {  
  15.     try   
  16.     {  
  17.         if(vFileName !="")return new Voice(vFileName)  
  18.         esle return 0;  
  19.     }  
  20.     catch(... )  
  21.     {  
  22.             delete pImg;  
  23.             throw;  
  24.     }  
  25. }  
这样在调用构建Voice对象中加入try...catch...用于释放pImg所占用的内存空间。其实有一个比其更简单的方法就是使用智能指针。
[cpp]  view plain copy
  1. const auto_ptr<Image> pImg;  
  2. const auto_ptr<Voice>  pVoi;  
  3.   
  4. People::People( const std::string& n,const int& a,const int& h,const std::stirng& imgFileName,const std::string& vFileName )  
  5. :name(n),age(a),height(h),  
  6. pImg( imgFileName !="" ? new Image( imgFileName ) : 0 ),  
  7. pVoi( vFileName != "" ? new Voice( vFileName ) : 0)  
  8. {}  
那么问题就算解决了,因为当其中有一个创建失败,离开函数的时候,智能指针会自动删除已经创建的空间,防止内存泄漏了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值