In my experience over the last couple of decades leading and managing development projects implemented in C and C++, pointer problems are by far the most common and hardest to identify kinds of defects. They are common because the pointer metaphor is very low level and requires precise management, but it is easy to forget about when you are dealing with all possible execution paths. Inevitably, it seems, a pointer is destroyed (or goes out of scope) but the memory is not properly freed (a memory leak), memory is released but nevertheless accessed (dangling pointer), or memory is accessed but not properly allocated (uninitialized pointer). These problems are notoriously difficult to identify using standard means of testing, and peer reviews and tools such as Purify[5] and LINT[6] that can identify "questionable practices" sometimes flag so many things it is virtually impossible to use the results. The smart pointer pattern is a mechanistic approach that has produced excellent results.
9.6.1 AbstractPointers are by far the most common way to realize an association between objects. The common implementation approach is whenever there is a navigable association, the object uses an attribute of type pointer, and this pointer is dereferenced as necessary to send messages to (i.e., invoke member functions on) the target object. The problem with pointers is that they are not objects; they are just data. Because they are not objects, the primitive operations you can perform on them are not checked for validity. Thus, we are free to access a pointer that has never been initialized or after the memory to which it points has been freed; we are also free to destroy the pointer without releasing the memory, resulting in the loss of the now no-longer-referenceable memory to the system. The smart pointer pattern solves these problems by making the pointer an object. A Smart Pointer object can have constructors and destructors and operations that can ensure that its preconditional invariants (rules of proper usage) are maintained. 9.6.2 ProblemIn many ways, pointers are the bane of the programmer's existence. If they weren't so dang useful, we would have discarded them long ago. Because they allow us to dynamically allocate, deallocate, and reference memory dynamically, they form an important part of the programmer's toolkit. However, their use commonly results in a number of kinds of defects:
These problems arise because pointers are inherently stupid. They are only data values (addresses) and the operations defined on them are primitive and without checks on their correct use. If only they were objects, their operations could be extended to include validity checks and they could identify or prevent inappropriate use. 9.6.3 Pattern StructureThe basic solution of the smart pointer pattern is to reify the pointer into an object. Once a pointer comes smart, or potentially smart, its operations can ensure that the preconditions of the pointer (i.e., it points to valid memory) are met. Figure 9-9a shows the simple structure of this pattern and Figure 9-9b shows a common variant. Figure 9-9a. Basic Smart Pointer PatternFigure 9-9b. Wrapper Variant9.6.4 Collaboration Roles
9.6.5 ConsequencesThe advantages of the application of this pattern is that it is a simple way to ensure that objects are destroyed when they are no longer accessible that is, when all references to them have been (or are being) destroyed. This requires some discipline on the part of the programmers. If the Target object is being referenced by both smart and raw pointers, then this pattern will break, with potential catastrophic consequences. On the other hand, the pattern can be codified into an easily checked rule: Use no raw pointers, and validate during code reviews. To ensure robustness in the presence of multithreaded access to an object (i.e., Smart Pointers exist in multiple threads that reference the same Target), care must be taken in the constructors and destructor. The simplest way to handle them is to make them atomic (i.e., prevent task-switching during the construction or destruction of a Smart Pointer). You can easily do this by making the first operation in the constructor a call to the OS to prevent task-switching (just don't forget to turn it back on when you're done!). The destructor must be similarly protected. Otherwise, there is a possibility that the object may be destroyed after you checked that it was valid and thus that a Smart Pointer is now pointing to a Target that no longer exists. Finally, there is one situation in which Smart Pointers may be correctly implemented but still may result in memory leakage. The Smart Pointer logic ensures that whenever there is no Smart Pointer pointing to a Target, the Target will be deleted. However, it is possible to define small cycles of objects that contain Smart Pointers, but the entire cycle cannot be accessed by the rest of the application. In other words, it is still possible to get a memory leak if the collaboration of objects has cycles in it. Figure 9-10 shows how this can happen. Figure 9-10. Smart Pointer CyclesIn Figure 9-10, object Obj3 and Obj5 form a cycle. If Obj2 and Obj4 are destroyed, the reference counts associated with Obj3 and Obj5 decrement down to 1 rather than 0, and these two objects are unable to be referenced by the remainder of the application. Since their reference counts are greater than 1, they cannot be destroyed, but neither can the application invoke services of these objects because there are no references to these objects outside the cycle itself. The easiest way to handle the problem is to ensure than no Target itself references another object that could even reference the original. This can usually be deduced from drawing class diagrams of the collaborations and some object diagrams resulting from the execution of the class diagram. If cycles cannot be avoid, then it might be better to avoid using the smart pointer pattern for those cycles specifically. 9.6.6 Implementation StrategiesThis pattern is simple and straightforward to implement and should create no problems. If you desire a smart pointer pattern that can handle cyclic object structures, then this can be solved at the cost of increased complexity and processing resource usage. A good discussion of these methods is provided in [9], Chapter 3. 9.6.7 Related PatternsMore elaborate forms of the Smart Pointer object are described in [9], although they are expressed as algorithms defined on the Smart Pointer rather than as patterns per se, as is done here. When cycles are present but the benefits of the smart pointer pattern (protection against pointer defects) are strongly desired, the garbage collector or compacting garbage collector patterns [8] may be indicated. 9.6.8 Sample ModelFigure 9-11 shows a simple application of this pattern. Two clients of the HR Sensor object exist: One object that displays the values and another that tracks values to do trending analysis. When the HR Display object runs, it creates an HR Sensor object via a Wrapped Sensor object. The HR Display object also notifies the HR Trend object to begin tracking the heart rate information (via the Wrapped Sensor object). Figure 9-11a. Smart Pointer Pattern StructureFigure 9-11b. Smart Pointer Pattern ScenarioLater, the HR Display is destroyed. It calls the delete operation on the WrappedSensor class. The WrappedSensor decrements its reference count but does not delete the HR Sensor because the reference count is greater than zero (the HRTrendDB still has a valid reference to it). Later on, when the HRTrendDB removes the last pointer to the HR Sensor object, the HR Sensor object is finally deleted. |