Chapter 3. Resource Management (Resource Management)
By Scott Meyers
Translator: fatalerror99 (itepub's nirvana)
Release: http://blog.csdn.net/fatalerror99/
Resource is something like this: once you stop using it, you need to return it to the system. If you do not do this, it will do nothing without harm. In C ++ProgramMedium, the most commonly used resource is dynamically allocated memory (dynamically allocated memory) (if you allocate memory but never release it, it will lead to memory leak (Memory leakage) consequences ), however, memory is only one of many resources that you must manage. Other common resources include file descriptors (file descriptor), mutex locks, fonts and brushes in graphical user interfaces (guis ), database connections (database connection) and network Sockets (network socket ). Regardless of the resource, releasing it is very important when you no longer use it.
It is difficult to make sure that this is done by hand under any conditions, but when you consider exceptions, a function with multiple return paths, without fully understanding the impact of the modifications he made, he should change the maintenance programmer of the software and handle resource management) this situation becomes very clear when the special methods are inadequate.
This chapter is based on C ++'s support for constructors, Destructors, and copying operations, object-based (Object-based) Resource management method. Experience shows that strict adherence to this method can eliminate almost all resource management problems. Next, we will move this chapter to the item focusing on Memory Management (memory management. These subsequent items supplement the previous more general items, because the objects (object) for memory management must know how to work correctly.
This chapter includes the following content. Click to open it:
Item 13: use objects to manage resources
Item 14: Think carefully about copying behavior in Resource-managing classes
Item 15: Provide access to raw resources in Resource-managing classes
Item 16: use the same form in corresponding uses of new and delete
Item 17: store newed objects in smart pointers in standalone statements