Previous Section  < Free Open Study >  Next Section

Leak #8

Forgetting to make the destructor of a base class virtual. The eighth memory leakage error with which a C++ developer needs to be concerned involves the use of virtual destructors. Developers will often use inheritance to create a taxonomy of classes for the purpose of treating the very different leaf classes as a homogeneous set (e.g., treating the very different apple, banana, and orange objects as a homogeneous collection of fruit). This homogeneous collection of fruit is implemented as an array of fruit pointers, where each fruit pointer points at a particular type of fruit object (i.e., an apple, banana, or orange object). Eventually, the delete function must be called on each of the fruit pointers in order to destroy the fruit type to which it points. The main question is which destructor will be called when the statement, delete basket[i] is invoked at runtime (assume basket is the array of fruit pointers). If the destructor for fruit, namely, Fruit:: ~Fruit(), is not virtual, then each time the delete statement is invoked, it is calling the destructor for fruit. If the destructor for fruit is marked virtual (polymorphic), then the correct destructor for the particular type of fruit is invoked at runtime (i.e., Apple objects call Apple::~Apple(), Banana objects call Banana::~Banana()). If any of the leaf classes (in this example, apples, bananas, and oranges) contain pointers to dynamically allocated space, then a nonvirtual base class destructor (in this example, Fruit::~Fruit) will result in memory leakage, since the destructor for the leaf class is never called (see Figure B.3). The following example illustrates this problem, using Fruit, Apple, and Banana.

Figure B.3. Graphical example of memory leakage due to a nonvirtual destructor (memory for "Rome", "Cuba", and "Mac" is leaked off the heap).

graphics/bfig03.gif

    Previous Section  < Free Open Study >  Next Section