of paper «A "Fruity" Approach to Memory Management in C++» see also paper's TOC
As I started programming a tiny library with classes for symbolic mathematics, I realized that C++ lacks support for easy handling of polymorphic class hierarchies. When using polymorphic class hierarchies, one immediately has to deal with pointers and dynamic memory allocation. Static objects cannot be used with polymorphism and abstract base classes. In most other OOP languages, an "object" is internally a "pointer to an object". This is different in C++, where statically allocated objects are possible.
From a design point of view, many of these cases have nothing to do with either pointers, or with dynamic memory allocation. Therefore a pattern was created that supports dealing with polymorphic objects as if they were static. The user of such an object needs not take care of memory allocation and deallocation, dereferencing or the distinction between shallow and deep copy. This done by introducing a classSmartPointer, that supports memory management with reference counting. Another pair of classes, a Shell and a Fruit, and some macros, support a so-called copy-on-write strategy, automatic construction and protection against use of uninitalized variables. The SmartPointer class is reused in the Shell class.
The extra level of indirection also provides some additional advantages such as separation of class hierarchy and interface class, possibility of assignment to oneself with type-change, and possibility of separate compilation. My approach is strongly connected to C++ ability of automatic constructor's/destructor's invocation. It is an alternative to operating-system based garbage collection.
The report shows in detail, why this technique is needed, what advantages it provides and how the pattern is used. It also contains a chapter about garbage collection in general, and some annotations to C++.
keywords: pattern: handle, letter/envelope, composite; garbage collection; C++; symbolic math library