Hi all,
I’ve got an issue with my app, I constantly fall into the same issues with either ReferenceCountedObject
s having leftover references, or leaks because of the ReferenceCountedObject
s not being deleted.
This is mainly because I have structured objects in my app through a ValueTree
, where the MainComponent
creates the topmost component of the app, which has the topmost ValueTree
in the structure. Then, when other components are added, they themselves have ValueTree
s which are children of the topmost one - and each ValueTree
in the system has a property which is a pointer to the component. This pointer is the source of my issues. The components are ReferenceCountedObject
s so that the pointer can be a ValueTree
property.
In the destructor of my object, I try and remove the object property before the actual destructor takes place. If I don’t, then there will be a leftover reference, in the tree, pointing to the object whose destructor has just been called.
(And the problem with that is..)
Even if the tree is destroyed with the object, the ReferenceCountedObject
pointer inside the actual tree is removed after the object’s pointer is made useless - which means that the decReferenceCount()
method inside the actual juce_ReferenceCountedObject
implementation tries to decrement a nullptr
, throwing a SegFault. (Should I let this pass?)
Therefore I chose to take an alternative route. In the destructor, I call removeAllProperties()
on my ValueTree, but since this would decrease the referenceCount to 0, I use the unholy incReferenceCount()
and decReferenceCountWithoutDeleting()
directly before and after.
All this mess seemed to work well, but I often end up with new problems when restructuring or adding in extra systems. I feel like I’m not taking the best route. Does anyone have tips on how I could structure this better?