through more than one base class, and each path will contribute another hidden pointer. Your code also depends on the fa
ct that reference-counted objects are always allocated on the heap, but this requirement is neither enforced by your classes nor documented in the article. For example,
#include "rgc.h"
class myclass:
public virtual TRGCObj {};
typedef TRGCRef
Pmyclass;
void main()
{ myclass m;
Pmyclass A = &m;
}
// m will be deleted here, but
// m is not on the heap!
yields undefined (and typically disastrous) behavior. This doesn't mean that reference counting is a bad technique or that your approach isn't useful. But programmers need to understand the drawbacks before adopting the technique.
Scott Meyers
Author, More Effective C++ (Addison-Wesley, 1996)
smeyers@netcom.com
The RGC garbage-collection scheme does increase the size of each object. This is unavoidable with reference counting, as you must have an extra integer to store the count. Virtual
inheritance worsens the problem. You can eliminate virtual inheritance if your class tree is structured such that each class inherits from
TRGCObj
only once, and thereby save some memory. Regardless, if your program uses a great many objects, it's important to consider that each object is at least one word larger when using any reference-counting garbage collector. As you also point out, RGC does require that objects be allocated from the heap; that is, with new, and not-as-local, variables. Thanks for mentioning these two important points.--Justin Miller, jcmiller@MIT.EDU