Effective C + + reading notes clause 14 managing resources with objects

Source: Internet
Author: User

Suppose 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 edition, there may be a memory leak, such as an exception occurred after calling Createinvestmen () to the function body between delete PINV, or call return, etc.//Then the DELETE statement will not be executed, as 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, there may be a memory leak, such as an exception between the function body after the call to Createinvestmen () to the delete PINV, or a call to return, etc.

Then the DELETE statement will not be executed, as memory leaks

<span style= "color: #3333ff;" >void f () {investmentfactory fac;investment* PINV = fac.createinvestment ();d elete PINV;} </span>


Second Edition: In order to ensure that the resources returned by Createinvestment are always released, we need to put the resources in the object, and when the control flow leaves F, the object's destructor will automatically release those resources
Auto_ptr is a class pointer object, the so-called "smart Pointer," whose destructor automatically calls delete on the object it refers to.

<span style= "color: #3333ff;" >void f () {investmentfactory fac;std::auto_ptr<investment> Pinv (Fac.createinvestment ());//Call factory function, As always, using PINV, the Auto_ptr function is automatically deleted through the destructor pinv}</span>

Two key ideas for "Managing resources with Objects" in the second edition above

1: Put the management object immediately after the resource is obtained: The above code createinvestmen the returned resource as the initial value of its manager auto_ptr. The concept of object resource management is also known as
Resource acquisition is actually the time to initialize, because we always get a resource and then initialize a management object with it within the consent statement.
2: Management objects use destructors to ensure that resources are released


Problems with the second edition:
Since Auto_ptr is destroyed automatically when it is deleted, it is important to be careful not to have multiple auto_ptr pointing at an object at the same time, and if so, the object will be deleted more than once, the program
Undefined behavior occurs, in order to prevent this problem, auto_ptr has an unusual nature: if they are copied through the copy constructor or the copy assigment operator, they become null
The copied pointer obtains the unique ownership 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, using PINV, the shared_ptr function automatically deletes pinvtr1::shared_ptr<investment> pInv2 (PINV1);//pinv1 and PInv2 point to a resource object pInv2 = pinv1;//ibid., without any changes//when PINV1 and PInv2 are destroyed, the objects they refer to are also automatically destroyed}</span>

/*
The third edition can be used normally and is the best version. But note that auto_ptr and shared_ptr are all done in destructors, not delete[], so don't
The dynamically allocated array is used on auto_ptr and shared_ptr.
*/

Summarize:

To prevent resource leaks, use object management resources, which derive resources from constructors and release resources in destructors

Two commonly used Raii class is tr1::shared_ptr and Auto_ptr; the first is the best choice because its copy behavior is more intuitive, if you choose Auto_ptr Copy action will make it point to null;

Effective C + + reading notes clause 14 managing resources with objects

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.