CS 12 -- Project 4 / Lab 9


Reference counting in C++

We want to make it possible for C++ programs to have objects perform reference counting so that the objects delete themselves when they become unreachable by any pointer. No approach to this problem is perfect, but we will attempt a strategy that, by using templates, should work for any object without modification to its class.


Reference counting pointers and objects

To perform reference counting, we need to impose two important changes:

  1. Each object must store the number of pointers that lead to it.
  2. A pointer must register with the object at which it points, and unregister with that object when it stops pointing at it.

To achieve these ends, we must create two new classes:

  1. RCObject -- This template class extends any other class to store a reference count within it. It should provide the following capabilities:
  2. RCPointer -- A template class that points at an RCObject. One of these ``smart'' pointers works much like a regular pointer, except that it perform the following tasks:

Your assignment is to create these two classes. You will begin by grabing copies of the following files:

~sfkaplan/public/cs12/Tester.cc
~sfkaplan/public/cs12/Point.hh
~sfkaplan/public/cs12/Point.cc

Within Tester.cc is code that creates Point objects. It does so using RCObject and RCPointer so that Point objects that are no longer reachable delete themselves.

Your assignment: Create the RCObject and RCPointer classes. They must be written so that the code in Tester.cc compiles and runs. I strongly recommend that your new classes emit debugging information that indicates where the RCPointer objects point, and how the reference counts of RCObject objects change. These debugging statements should make it clear when an object deletes itself due to a zero reference count.


C++ header file issues

First, it is important to remember that template classes must be wholly contained in header files. Thus, you should be placing your code in two files: RCObject.hh and RCPointer.hh.

Second, you will find that RCPointer relies on RCObject. That is, consider the following line from Tester.cc:

RCPointer<Point> p1 = new RCObject<Point>;

Although the RCPointer is really going to point at an RCObject<Point>, we only pass the Point class as a template argument to RCPointer. It is simply assumed by RCPointer that the Point object will really be an RCObject<Point> object.

Consequently, Tester.cc includes only RCPointer.hh. In doing so, both header files are actually included.

Please note that this is not the normal or best solution to the problem of multiple inclusion. It works for this case, and should be sufficient for this project. If you are interested, however, to learn about the better solutions, just ask me.


Submitting your work

Use the cs12-submit program to submit only your source code, which should have been written as RCObject.hh and RCPointer.hh files. Submit your work as project-4.


This assigment is due on Thursday, December 18th (the last day of exams) at 5:00 pm!

Scott F. Kaplan
Last modified: Thu Dec 4 09:52:32 EST 2003