Reference Counting

The reason we have to set m_Count to 1 in the UndatedStockItem variable pointed to by m_Worker is that we're going to keep track of the number of StockItems that are using that UndatedStockItem worker object, rather than copy the worker object every time we copy a StockItem that points to it, as we did with our string class. This approach to sharing an object is called reference counting.

The general idea of reference counting is fairly simple, as most great ideas are (after you understand them, at least). It's inefficient to copy a lot of data whenever we set one variable to the same value as that of another; copying a pointer to the data is much easier. Even so, we have to consider how we will know when we can delete the data being pointed to, which will be when no one needs the data anymore. If we don't take care of this requirement, we'll have a serious problem with memory management. When one of the StockItem objects that refers to the worker object goes out of scope and is destroyed, the destructor can do either of the following, neither of which is correct:

  1. free the memory where the data is kept, via delete,

  2. fail to free the memory.

The first of these is incorrect because there may be other StockItems that still want to use the data in question, and they will now be referring to memory that is no longer allocated to that data. Therefore, at any time the data in that area of memory may be overwritten by new data. The second is also incorrect because when the last StockItem that was using the shared data goes away, a memory leak results. In other words, although the data formerly used by the StockItem can no longer be accessed, the memory it occupies cannot be used for other purposes because it has not been released back to the system via operator delete.[7]

[7] This is the same problem we saw with the compiler-generated copy constructor and the compiler-generated assignment operator for the string class we created earlier in this book. See the discussion beginning at the heading “Why We Need a Reference Argument for operator =” on page 470.

The correct way to share data in such a situation is to write the constructor(s), destructor, and assignment operator to keep track of the number of objects using a particular set of data and, when that set of data has no more users, to free the memory it occupies.[8] Let's see how reference counting works with our StockItem class.

[8] There is actually another requirement for correct sharing of data in this situation. We mustn't make changes to the data in a shared worker object that has more than one user. In the current application, we don't have that problem because we aren't changing the data after the objects have been created. However, this is something we'll have to deal with in Chapter 11, when we develop a polymorphic object type whose objects can be changed after creation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset