Effective C + + clause 13-17 "Object Management resources" shared_ptr analysis

Source: Internet
Author: User

As the name implies, Boost::shared_ptr is a smart pointer that can share ownership, first let's look at its basic usage in an example:
#include <string> #include <iostream> #include <boost/shared_ptr.hpp>class implementation{public:    ~implementation () {std::cout << "destroying implementation\n";}    void Do_something () {std::cout << "did something\n";}}; void Test () {    boost::shared_ptr<implementation> SP1 (new implementation ());    std::cout<< "The Sample now have" <<sp1.use_count () << "references\n";    Boost::shared_ptr<implementation> SP2 = SP1;    std::cout<< "The Sample now have" <<sp2.use_count () << "references\n";        Sp1.reset ();    std::cout<< "after Reset SP1. The Sample now has "<<sp2.use_count () <<" references\n ";    Sp2.reset ();    std::cout<< "after Reset sp2.\n";} void Main () {    test ();}

The output of the program is as follows:

The Sample now has 1 references
The Sample now has 2 references
After Reset SP1. The Sample now has 1 references
Destroying implementation
After Reset SP2.

As you can see, boost::shared_ptr pointers SP1 and SP2 both have access to the implementation object, and when both SP1 and SP2 release ownership of the object, the memory of the object it manages is automatically freed. The access rights of the shared object also enable automatic management of its memory.

BOOST::SHARED_PTR's memory management mechanism:

BOOST::SHARED_PTR management mechanism is not complex, is the object of the management of the reference count, when a new boost::shared_ptr to manage the object, the reference count of the object is added one; reduce a boost::shared_ When PTR manages the object, the reference count of the object is reduced by one, and if the reference count of the object is 0, it is not managed by any pointers, it is called Delete to release the memory it occupies.

The above example can be illustrated as follows:

SP1 manages the implementation object with a reference count of 1
Increase SP2 to manage implementation objects with a reference count of 2
SP1 release manages the implementation object, whose reference count changes to 1
SP2 release manages the implementation object, its reference count becomes 0, and the object is automatically deleted
Features of Boost::shared_ptr:

Boost::shared_ptr can share ownership of an object, so its scope of use is largely limited (there are some usage rules that need to be followed, described below), and it can also be used in STL containers. It's also thread-safe, which is also important in multithreaded programs.

BOOST::SHARED_PTR Rules of Use:
Boost::shared_ptr is not absolutely secure, and the following rules will make it safer for us to use BOOST::SHARED_PTR:

Avoid direct memory management operations on objects managed by shared_ptr to prevent the object from being re-released
SHARED_PTR does not automatically manage the memory of objects that are circular references (this is a common problem with various other reference counts for managing memory patterns).
Do not construct a temporary shared_ptr as a function parameter.
The following code can cause a memory leak:
void Test ()
{
Foo (boost::shared_ptr<implementation> (New Implementation ()), G ());
}
The correct usage is:
void Test ()
{
Boost::shared_ptr<implementation> SP (new Implementation ());
Foo (sp,g ());
}

Effective C + + clause 13-17 "Object Management resources" shared_ptr analysis

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.