If we use a library of investment behavior:
#include "stdafx.h" #include <iostream> #include <memory>using namespace Std;class investment{public: };class investmentfactory{public:virtual investment* createinvestment () {investment * InV = NULL;return inV;}};/ /first version, there may be a memory leak, for example, after calling Createinvestmen () to delete pinv between the function body between the exception or call return, etc.//Then DELETE statement will not be run, early memory leak void F () { Investmentfactory fac;investment* PINV = fac.createinvestment ();d elete PINV;} int _tmain (int argc, _tchar* argv[]) {return 0;}
In the first edition, a memory leak can occur, such as an exception occurred between the function body after the call to Createinvestmen () to the delete Pinv, or a return is called.
Then the DELETE statement will not be run, as memory leaks
<span style= "color: #3333ff;" >void f () {investmentfactory fac;investment* PINV = fac.createinvestment ();d elete PINV;} </span>
Second Edition: To ensure that the resources returned by Createinvestment are always released, we need to put the resources in the object. When the control flow leaves F, the object's destructor voluntarily frees those resources
Auto_ptr is a class pointer object, called a "smart pointer." Its destructor invokes delete on the object it is referring to on its own initiative.
<span style= "color: #3333ff;" >void f () {investmentfactory fac;std::auto_ptr<investment> Pinv (Fac.createinvestment ());//Call factory function, As always, use PINV. Delete the pinv}</span> by itself by Auto_ptr's destructor function
Two key ideas for "Managing resources with Objects" in the second edition above
1: Get the resources immediately put into the management object: The above code createinvestmen the returned resources are treated as the initial value of their manager auto_ptr.
The concept of object resource management is also known as
Resource acquisition is actually the time of initialization. Since we are always getting a resource, we initialize a management object with it within the allowed statement
2: Management objects use destructors to ensure that resources are released
Problems with the second edition:
Since Auto_ptr is destroyed and will voluntarily delete what it refers to, it is important to be careful not to have multiple auto_ptr point to an object at the same time. Let's just say that. Object will be deleted more than once, the program
There is no defined behavior, in order to prevent this problem, auto_ptr has an unusual nature: if they are copied by copy constructor or copy assigment operator, they will become null
The copied pointer gets the sole and exclusive right of the resource.
Auto_ptr<investmen> pInv1 (Fac.createinvestment ()); PINV1 pointing to resources
Auto_ptr<investment> pInv2 (PINV1); PInv2 pointer to resource PINV1 NULL
PINV1 = PInv2; PINV1 point to Object, PInv2 becomes null
Third edition:
<span style= "color: #3333ff;" >void f () {investmentfactory fac;std::shared_ptr<investment> pInv1 (Fac.createinvestment ());//Call factory function. As always, the use of PINV, through the shared_ptr destructor of their own initiative to delete pinvtr1::shared_ptr<investment> pInv2 (PINV1);//pinv1 and PInv2 Point to a Resource object pInv2 = pinv1;//Ditto, no matter what changes//when PINV1 and PInv2 destroyed, the objects they refer to are also voluntarily destroyed}</span>
/*
The third edition can be used normally and is the best version number. But notice that auto_ptr and shared_ptr in the destructor are all done by delete instead of delete[], so don't put
The dynamically allocated array is used on auto_ptr and shared_ptr.
*/
Summarize:
To prevent resource leaks, use object management resources, where they get resources in the constructor. and releasing resources in the destructor
Two frequently used RAII classes are tr1::shared_ptr and auto_ptr; the first is the best choice because its copy behavior is more intuitive, selecting the Auto_ptr copy action causes it to point to null.
Effective C + + reading notes clause 14 managing resources with objects