Effective C + + 3 resource Management

Source: Internet
Author: User

Resources are owned by the system and must be returned to the system after borrowing resources. The main resources include: memory, file descriptor, mutex, font in graphical interface, database connection, network socket.

article : Managing Resources with Objects

Memory that is managed by pointers in an object, such as memory allocated dynamically during a run , can easily be forgotten by the user, or it is difficult to grasp its release time, thus causing a memory leak, by using the object to manage resources to ensure its normal operation, that is, the use of smart pointers, The intrinsic method of automatically invoking the smart pointer when the object is destroyed to deconstruct the dynamic memory it points to.

There are two characteristics of managing resources with objects:

a) when a resource is acquired, it can be called a " Resource acquisition opportunity is an initialization opportunity " (Rall)because it is always used to initialize a management object within the same statement after acquiring a resource.

b) Management Objects use destructors to ensure that objects are freed. That is, the destructor is automatically called when the object is destroyed, and the resource is freed.

The main include pointer pointers: the memory that the destructor auto-release points to.

l pointer pointer includes AUTO_PTR, such as std::auto_ptr<sutdet> Pstu (Createstudent ()); Where student is the class name, where the type of smart pointer is represented, but when you copy this type of pointer by copying the constructor and the equals sign operator, the previous pointer becomes NULL, that is, there is always only one pointer to the object.

The alternative is to use the "Apply counting Smart pointer " (RCSP), such as tr1::shared_ptr, using the method:

such as std:: tr1::shared_ptr<sutdet> pstu (Createstudent ());

article : Careful copy behavior in the resource management class

The issue of what happens to a RAII object being copied is the call to the copy constructor. There are two possible ways to prevent replication and to apply a count to the underlying resource.

Class Lock

{

Public:

Explicit Lock (mutex* pm): mutexptr (Pm,unlock)// construct and unlock as a delete

{Lock (Mutexptr.get ());

}

Private:

Std::tr1::share_ptr<mutex> mutexptr;

}

mutexptr 's destructor automatically calls the tr1::shared_ptr 's Delete ( in this case, unlock)when the reference count of the mutex is 0 . Copying its objects increases its reference count.

Copying a RAII object must replicate the resources it manages, so the copying behavior of the resource determines the copying behavior of the RAII object.

article : Provide access to the original resource in the Resource management class

Both tr1::shared_ptr and auto_ptr provide a get member function to perform the display transformation, which means that the original pointer can be returned inside the smart pointer. Int day = Dayheld (pinv.get); Where PINV is a smart pointer.

This display transformation can also be encapsulated by an interface to make it easier to use, but may result in misuse of resource applications.

terms : Pairs use new and delete to take the same form

l When you use the new operator, two things happen. First: memory is allocated; second: One or more constructors are called for this memory. The same is the case when the delete is called, and the release is first destructor.

the array form used in the application should also be in the form of an array when released, and should be consistent.

terms and conditions: The newed object is placed into a smart pointer with a separate statement

The newed object is stored ( placed ) in a smart pointer with a separate statement. If you do not, once an exception is thrown, it is possible to cause an imperceptible resource leak.

Because you can then fix the execution order to allocate memory dynamically, call the tr1::shared_ptr constructor, call the function function. Otherwise, the order of execution of the second and third steps is not guaranteed.

such as Processwidget (std::tr1::shared_ptr<widget> (new widget));

should be converted to :

Std::tr1::shared_ptr<widget> PW (new widget);

Processwidget (PW);

Effective C + + 3 resource Management

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.