Overengineering ReferenceCountedObject


Oh you thought that ReferenceCountedObject couldn’t be overengineered? Wrong!

If your ReferenceCountedObject is a heavy class that uses a thread, and expensive to delete, then why put the burden of destroying the object on the poor guy who owns the last reference? Just have the thread in the class delete itself when the last reference is gone. Of course, this requires a small change to ReferenceCountedObject:

class ReferenceCountedObject { public: // ... inline void decReferenceCount() throw() { jassert (getReferenceCount() > 0); if (--refCount == 0) destroyReferenceCountedObject (); } protected: // ... virtual void destroyReferenceCountedObject() { delete this; /* subclasses may override */ } // ... };

I’m not saying this needs to be changed in Juce…just sharing the technique.