C ++ garbage collection-manages memory with objects

Source: Internet
Author: User

 

1. Use objects to manager resources .)

This clause mainly describes how to apply for release of dynamic memory.

RaiIPrinciples:

Ø
Put the resource into the management object immediately after obtaining it;

Ø
Management Objects use destructor to ensure resources are released;

I also wrote a class for Dynamic Release application. One assumption of this class is that a resource will be used immediately after it is applied, and it needs to be released after the function returns. For this reason, I wrote a base class, and each resource class must inherit this base class. The constructor and destructor of each resource subclass are private, which eliminates the new and delete operations. Applying for a new object is implemented through the static function of the class, and the release function interface is not provided. The base class has a static list. The base class constructor will add this pointer to this list. The base class traverses the list every five cycles to see if there are any objects to be released. If so, release the objects directly.

Standard Library auto_ptrClass pointer object

It should be a template.

STD: auto_ptr <point> pinv (New
Point (1 ));

Point is the class pointed to by the class pointer object.

Pinv is the variable name of the class pointer. The reference of the class pointed to by the class pointer in the () following it.

Void R_test()

{

STD: auto_ptr <point> pinv (NewPoint (1 ));

 

Pinv-> getx ();

}

Then, pinv can be directly used as a point pointer. But it is a local variable and will be released when the function returns. When it is released, the point destructor will be performed.

UseAuto_ptrBe sure to prevent multiple pointers from pointing to an object. This will cause multiple releases. So he has a property: if they are copied through the copy constructor or the copy assignment function, they will themselves become null, and the resulting pointer will obtain the unique ownership of the resource.

Note:Auto_ptrThe copy assignment function and the copy constructor are highly risky. Use less.

Reference counting smart pointer (rcsp))

This does not seem to exist in Linux (header files need to be included)# Include<Tr1/memory> ).

Note:

To prevent resource leakage, useRaiI(The origin retrieval time is the initialization time-resources are obtained during the construction and released during the analysis)They get resources in the constructor and release resources in the destructor. (Template technology used)

The two commonly used raiiclass are tr1: shared_ptr and atuo_ptr. The former is usually a better choice, because copy is more intuitive. If you select atuo_ptr, the copy operation will point it to null.

 

2. Copying behavior (think carefully about copying behavior in resource-management)
Classes .)

If tr1: shared_ptr and atuo_ptr are not suitable for resource management, you need to construct your own resource management class.

When designing resource management, you also need to follow the raiI principles:

Note that what happens when a raiI object is copied:

1,
Prohibit Replication

2,
Use reference count for underlying resources. Generally, a resource management class can implement reference-counting as long as it contains a tr1: shared_ptr member variable.
Copying behavior.

Tr1: shared_ptr: by default, the corresponding object will be deleted when the object is deleted. However, you can specify the so-called deleteer during the construction, is that a function or function object (rather than a simple object?What is a function object?) Is called when the number of application times is zero (this mechanism does not exist in auto_ptr -- it always deletes the pointer ).

3,
Copy the bottom resource. That is, perform the so-called deep copy.

4,
Transfer resource ownership-just like auto_ptr.

Note:

When copying a raiI object, you must copy the resources it manages. Therefore, the copying behavior of the resource determines the copy behavior of the raiI object.

Common raiiclasses
The copying action is to suppress copying and execute the reference counting method.

 

3, In the resource management class, provide the original resource access (provide access to raw resources in resource-management)
Classes .)

RaiI class requires a function. You can see that the raiI classes object is converted to the original resource contained in it.

Display conversion:

Tr1: Both shared_ptr and auto_ptr provide get member functions. Used to perform display conversion.

In addition, they provide operator-> and operator * operators for implicit conversion.

Use display conversion whenever possible.

The get function conflicts with the encapsulation, but the problem is not big. Raiiclass is mainly used to release resources.

"A well-designed class will hide what users don't need to see and prepare what customers need to see ."

Note:

APIS usually requires access to the original resources. Therefore, each raiiclass should provide a method to obtain the resources it manages.

Access to the original resource may be converted by display or implicit conversion. In general, display conversion is safer, but implicit conversion is more convenient.

 

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.