ValueTree is not static friendly


#1

For a demo purpose I wanted to make a static ValueTree.
Tested on macOS.
I didn’t get to the bottom of it but you can simply reproduce this doing the following -

  1. create a JUCE app.
  2. in some main cpp or h add your static object such as
static ValueTree staticVT { "ValueTree", {{"static", "constructor" }}};

On the compiled binary you’ll hit the following -

Another thing I’ve tried that failed was -
creating an empty juce::ValueTree and from my constructor:

if (!myValueTree.isValid())
{
    myValueTree = ValueTree { "ValueTree", {{"static", "constructor" }}};
}

This one could lead to leakage.

I know this isn’t a common use case for ValueTrees but thought it’s worth posting/reporting.


#2

I think any non-trivial statics are basically dangerous.
You can use the Meyers singleton pattern though? (I.e. wrap it in a function so it only gets initialised on the function call).


#3

Thanks, our need was just on some sample code. and the new constructor looked cleaner.

Eventually a static ValueTree foo (“foo”);
and setting it up on our instance constructor was working. but code wasn’t clean as both snippets I’ve posted.
(we didn’t want to have too much code for this example).

I wondered but didn’t find it there’s a way to set a class to don’t allow static instances of it. but that’s another story.