Unsafe ValueTree Constructor

Hi!
I just stumbled upon an unsafe way to initialise a ValueTree. And I think, JUCE could catch this unsafe initialisation. With an assert.

So, there are several ValueTree constructors. The one, which lets you initialise several properties at once, is unsafe.
The problem with it: It does not check, if you are giving it the same property name twice. I did that by mistake. And the strange result is: The property appears twice in the value tree. But some ValueTree functions pick up only the first instance, and others only the second instance. So this results in a “shadowing” of some kind.

Try to run this code:

ValueTree tree("MyTreeName", {{"MyProperty_A", 32},
                              {"MyProperty_B", false},
                              {"MyProperty_A", 32} });
        
DBG("\n--------------\nStarting Tree Test:");

tree.setProperty("MyProperty_A", 4, nullptr);
DBG("MyProperty_A = " + tree.getProperty("MyProperty_A").toString());
DBG("Whole tree: " + tree.toXmlString({}));

DBG("--------------\n\n\n");

Here is the output:

--------------
Starting Tree Test:
MyProperty_A = 4
Whole tree: <?xml version="1.0" encoding="UTF-8"?>
<MyTreeName MyProperty_A="32" MyProperty_B="0"/>
--------------

As you can see. I get two different values for “MyProperty_A”.
ValueTree::getProperty reports it as “4”. But ValueTree::toXmlString reports it as “32”.

Its of course my mistake. Cause I gave the constructor the same property twice.

But could the JUCE team add an assert for this type of behaviour? So that silly programmers like me cannot make that mistake :slight_smile:
That would be awesome!

Note: I am on JUCE 5.4.7 in OSX Catalina.

1 Like