Resources: Dynamically allocated memory, file descriptors, mutexes, fonts and brushes in the graphical interface, database connections, and network sockets, whatever the resource, it is important that you return it to the system when you no longer use it.
Article 13: Managing Resources with objects
When we apply for resources to the system, we must remember to release, otherwise it will be prone to memory leaks. But it is not easy to realize that such a thing is, for example, we are using a function to dynamically allocate memory and return a pointer.
Investment* Ceateinvestment (); Returns a pointer to the dynamically allocated object F () { investmentPINV;}
Even if, like the code above, we call the delete after we run out of pointer Pinv, we may still have problems, and sometimes we might be in ... Part to return or jump out of the loop, or even an exception occurred in that section, which causes the delete to not execute at all.
The workaround is that we put the pointer in a resource-managed class so that the class object automatically calls the destructor at the end of the life, and the destructor executes the delete.
The two commonly used RAII classes are: shared_ptr and unique_ptr, which differ shared_ptr allow multiple pointer copies of the same memory area, whereas UNIQUE_PTR allows only one pointer to point to the object when Uniuqe_ When PTR takes an assignment operation, the pointer used to assign the value becomes null.
Article 14: Beware of copying behavior in resource management classes
In the actual management of resources, not all resources are heap memory (heap), so smart pointers such as unique_ptr or shared_ptr are often unsuitable as resource managers.
For example, you need to control the mutex object of type Mtux, altogether the lock and unlock two functions are available, and you need to ensure that you do not forget to unlock a locked mutex, and our idea is that the resource is created during construction and released during the destruction.
Lock{public: pm): Mutexptr (pm) {lock (mutexptr;)} ~lock () {unlock (mutexptr);} };
This is fine, but if lock is copied, a problem occurs. It may cause a mutex to be unlocked two times.
There are 2 common solutions: One is prohibit replication, the second is the "reference counting" method for the underlying resource, which is also the shared_ptr implementation principle.
Article 15: Provide access to the original resource in the resource management class
Many API interfaces often require access to raw resources, so each RAII class should provide a way to "get the resources it manages", such as providing a Get function.
Access to the original resource may be explicitly converted or implicitly converted. Explicit conversions are generally more secure, but implicit conversions are more convenient for customers.
Article 16: Paired use new and delete with the same form
If you use [] in the new expression, you must use [] in the corresponding delete expression. If you do not use [] in the new expression, you must not use [] in the corresponding delete expression.
Stringstring; Stringstring[+]; Delete an object delete//delete an array consisting of objects
Article 17: Placing a Newed object into a smart pointer with a standalone statement
Let's say we have a function to control the priority of the program, and another function to perform some priority handling on a dynamically allocated wieget:
Priority (); Processwidget (shared_ptr priority);
Now if we call it this way:
Processwidget (share_ptr<widget> (widgets), priotrity ());
Before calling Processwidget, the compiler must create code that does the following three things:
- Call priority
- Execute "New Widget"
- Call the shared_ptr constructor
The C + + compiler does not guarantee the order in which the last code was executed, but one thing is to be sure that the new widget must have occurred before the Share_ptr constructor.
If priority occurs in the second step and an exception occurs during execution, there is a chance that the resource will not be released properly.
To avoid this, you need to use the detach statement, create the widget first, then place it in a smart pointer, and then pass that smart pointer to Processwidget.
"Effective C + +" resource management