ReferenceCountedObjectPtr and ReferenceCountedArray

Any reference counted class instance must be inherited from ReferenceCountedObject. I assume the base class then holds the ref counter variable? Does this mean that the following the is legal to do and doesn’t lead to bugs:

ReferenceCountedObjectPtr<Something> pointer(new Something());

Something* temp_pointer = pointer.get();

ReferenceCountedObjectPtr<Something> pointer_2 = temp_pointer;

I.e. Does the reference counting work (or does it get messed up) if I move the reference couted object through a regular pointer and create a new ref counted pointer from that?

Yes, unlike shared_ptr, ReferenceCountedObject holds the reference count in the base class (i.e. intrusively), which means that the code you wrote should be fine. You can step through it in a debugger and look at the reference count just to be sure.

ReferenceCountedObject is pretty good at telling you right away when you’re doing something illegal, so unless you hit an assertion, you can probably assume you’re fine. (You can also add a JUCE_LEAK_DETECTOR to you class for extra protection.)