http://blog.csdn.net/couhujia/article/details/8474905
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等没有使用相应的函数释放掉,导致系统资源的浪费,严重可导致系统效能降低,系统运行不稳定。
首先说说标题可能取得有些大,但是可以理解为编程过程中有效的防止写的代码中有内存泄漏。好了废话不多说了,首先看下面一段代码。
- class Image
- {
- public:
- Image(const std::string& imgFileName);
- ...
- }
- class Voice
- {
- public:
- Voice(const std::string& vFileName);
- ...
- }
- class People
- {
- public:
- People(const std::string& n,const int& a,const int& h,const std::stirng& imgFileName,const std::string& vFileName);
- ~People( );
- private:
- string name;
- int age;
- int height;
- Image *pImg; //图像
- Voice *pVoi; //声音
- };
- People::People( const std::string& n,const int& a,const int& h,const std::stirng& imgFileName,const std::string& vFileName )
- :name(n),age(a),height(h),pImg(0),pVoi(0)
- {
- if(imgFileName != "")
- {
- pImg = new Image( imgFileName ) ;
- }
- if(vFileName != " ")
- {
- pVoi = new Voice ( vFileName );
- }
- }
- People::~People( )
- {
- delete pImg;
- delete pVoi;
- }
- ...
- People *p =NULL;
- try{
- p=new People("test",20,170,".../images/image01.jpg","../voices/voice01.dat");
- ...
- catch( ... ){
- delete p;
- throw;
- }
- delete p;
- }
仔细想想 new People("test",20,170,".../images/image01.jpg","../voices/voice01.dat")里,如果最后为Image分配的内存被丢失,因为new操作没有成功完成,程序不会p进行赋值操作。所以catch中是没有任何操作的,已被分配的内存就丢失了。
因为对象在构造中抛出异常后C++不负责清除对象,所以我们需要重新设计构造函数让它们在运到异常的时候自己能清除所占用的内存。
- People::People( const std::string& n,const int& a,const int& h,const std::stirng& imgFileName,const std::string& vFileName )
- :name(n),age(a),height(h),pImg(0),pVoi(0)
- {
- try {
- if(imgFileName != "")
- {
- pImg = new Image( imgFileName ) ;
- }
- if(vFileName != " ")
- {
- pVoi = new Voice ( vFileName );
- }
- }
- catch( ...) {
- delete pImg ;
- delete pVoi;
- throw ;
- }
- Image * const pImg; //图像
- Voice * const pVoi; //声音
- People::People( const std::string& n,const int& a,const int& h,const std::stirng& imgFileName,const std::string& vFileName )
- :name(n),age(a),height(h),
- pImg( imgFileName !="" ? new Image( imgFileName ) : 0 ),
- pVoi( vFileName != "" ? new Voice( vFileName ) : 0)
- {}
- People::People( const std::string& n,const int& a,const int& h,const std::stirng& imgFileName,const std::string& vFileName )
- :name(n),age(a),height(h),
- pImg( initImage( imgFileName ) ),
- pVoi( initVoice( vFileName ) )
- {}
- Image* People::initImage(const string& imgFileName)
- {
- if(imgFileName !="") return new Image(imgFileName);
- else return 0;
- }
- Voice* People::initVoice(const string& vFileName)
- {
- try
- {
- if(vFileName !="")return new Voice(vFileName)
- esle return 0;
- }
- catch(... )
- {
- delete pImg;
- throw;
- }
- }
- const auto_ptr<Image> pImg;
- const auto_ptr<Voice> pVoi;
- People::People( const std::string& n,const int& a,const int& h,const std::stirng& imgFileName,const std::string& vFileName )
- :name(n),age(a),height(h),
- pImg( imgFileName !="" ? new Image( imgFileName ) : 0 ),
- pVoi( vFileName != "" ? new Voice( vFileName ) : 0)
- {}