9.6 Smart Pointer Pattern


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.

[5] See http://www.rational.com/products/purify_nt/index.jsp for a commercial product description.

[6] See http://www.gimpel.com/ for a commercial product description.

9.6.1 Abstract

Pointers 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 Problem

In 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:

  • Memory Leaks: Destroying a pointer before the memory they reference is released. This means that the memory block is never put back in the heap free store, so its loss is permanent, at least until the system is rebooted. Over time, the available memory in the heap free store (i.e., memory that can now be allocated by request) shrinks and eventually the system fails because it cannot satisfy memory requests, even though it ought to be able to.

  • Uninitialized Pointers: Using a pointer as if it were pointing to a valid object (or memory block) but neglecting to properly allocate the memory. This can also occur if the memory request is made but refused.

  • Dangling Pointers: Using a pointer as if it were pointing to a valid object (or memory block) but after the memory to which it points has been freed.

  • Pointer Arithmetic Defects: Using a pointer as an iterator over an array of values, but inappropriately. This can be because the pointer goes beyond the bounds of the array (in either direction), possibly stepping on memory allocated to other objects, or becoming misaligned, pointing into the middle of a data value rather than at its beginning.

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 Structure

The 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 Pattern

graphics/09fig09.gif

Figure 9-9b. Wrapper Variant

graphics/09fig09_01.gif

9.6.4 Collaboration Roles

  • Client: The Client is the object that at the analysis level simply has an association to the Target object. If this is a bi-directional association, then both these objects have smart pointers to the other.

  • SmartPointer: The Smart Pointer object contains the actual pointer (rawPtr) as an attribute, as well as constructor, destructor, and access operations. The access operations will usually be realized by overriding pointer dereference operators ([] and ->) in C++, to hide the fact that a smart pointer is being used. The Target::referenceCount attribute keeps track of the number of smart pointers that are referring to the specific target object. This is necessary to know when to destroy the dynamically created Target.

    The Smart Pointer object has two constructors. The default constructor creates a corresponding Target object and sets referenceCount to the value 1. The second constructor initializes the rawPtr attribute to the value of the address passed in and increments the Target::referenceCount. The destructor decrements the Target::referenceCount; if it decrements to 0, the Target is destroyed. In principle, the Target::referenceCount must be referred to by all Smart Pointer objects that point to the same object.

  • Target: The Target object provides the services that the Context object wishes to access. In the basic form of the pattern (a), the Target object also has a reference count attribute that tracks the number of Smart Pointer objects currently referencing it.

  • TargetWrapper: In the smart pointer pattern variant b, the Target object is not at all aware of Smart Pointers or reference counts. The TargetWrapper object contains, by composition, the Target object, and owns the referenceCount attribute.

9.6.5 Consequences

The 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 Cycles

graphics/09fig10.gif

In 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 Strategies

This 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 Patterns

More 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 Model

Figure 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 Structure

graphics/09fig11.gif

Figure 9-11b. Smart Pointer Pattern Scenario

graphics/09fig11_01.gif

Later, 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.



Real Time UML. Advances in The UML for Real-Time Systems
Real Time UML: Advances in the UML for Real-Time Systems (3rd Edition)
ISBN: 0321160762
EAN: 2147483647
Year: 2003
Pages: 127

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net