Common resources include memory, file descriptor, mutex locks, fonts and brushes in the graphic interface, database connections, and network sockets. These resources are generally dynamically created and allocated, which is a pointer. Regardless of the resource, it is important that you must return it to the system when it is no longer used.
Clause 13: object-oriented resource management
By putting resources into the object, we can rely on the automatic calling mechanism of the c ++ destructor to ensure that resources are released. At the end of the scope, the Destructor automatically calls Delete For the pointer to the resource (object) it refers.
C ++ provides two classes for resource management: "smart pointer" STD: auto_ptr, and "reference counting smart pointer" STD: tr1: shared_ptr. They are called smart pointers, but in essence they are pointer-like objects. member variables are pointers that characterize resources. The two are used in the same way:
1 class investment {...};
2 Investment * createinvestment ();
3
4 void F ()
5 {
6...
7 Std: tr1: shared_ptr <investment> pinv1 (createinvestment ());
8 // pinv1 points to the object returned from createinvestment
9 STD: tr1: shared_ptr <investment> pinv2 (pinv1 );
10 // both pinv1 and pinv2 now point to the object
11
12 pinv1 = pinv2; // OK
13...
14}
Auto_ptr prevents multiple auto_ptr entries from pointing to the same object at the same time. If so, the object will be deleted more than once, which leads to "undefined behavior ". To prevent this problem, auto_ptr has an unusual nature: if you copy them through the copy constructor or the copy assignment operator, they will become null, the pointer obtained from the copy operation obtains the unique ownership of the resource. Remember, the STL container requires that the elements stored by the STL container have normal replication behaviors, so these containers cannot accommodate auto_ptr.
Shared_ptr has a normal replication behavior. It can be stored in STL containers.
Both of them execute delete on the resource pointer in the destructor, instead of Delete []. Therefore, it is a good idea to manage dynamically allocated arrays. Vector and string can almost always replace the dynamically allocated array.
The two have a common name, raiI object. To prevent resource leakage, use the raiI object. They obtain resources in the constructor and release resources in the destructor. Generally, shared_ptr is selected because its replication behavior is normal and intuitive. The auto_ptr copy action points to null.
In addition, tr1: shared_ptr allows you to create a resource release function when the smart pointer is set up (the so-called deleteer, "deleter ") bound to a smart pointer (auto_ptr does not have this capability ). When the number of references is 0, the deleteer is called.
Tr1: shared_ptr supports custom delimiters. Mutexes can be used to automatically remove mutex locks (see clause 14.
Article 17: Insert a newed object into a smart pointer using an independent statement
1 processwidget (STD: tr1: shared_ptr <widget> (new widget), priority (); // non-independent statement
2 STD: tr1: shared_ptr <widget> Pw (new widget); // independent statement
3 processwidget (PW, priority ());
In line 1, the compiler must perform three operations. The computing order compiler of the two parameters is flexible. If the priority function is executed after new and a function exception occurs, the newly generated widget object is not placed with a smart pointer, causing resource leakage. Rows 2 and 3 solve this problem.