Understanding Value Trees


#1

Hi all

I spent probably 8 hours yesterday trying to understand ValueTrees, TreeViews and TreeViewItems to no avail. I plan to have a global value tree where I can store values which controls sets data being received from external devices and sent via OSC messages. Is having a global value tree the way in which ValueTree is expected to be used, or is there a more JUCE-y way of setting out my project and the value trees?

edit: After further exploration I still can’t crack it. Any hints into what is wrong with the following code and why it gives me a bad access error?



#2

Why don’t you just declare a global variable like

ValueTree myGlobalValueTree;

#3

Your screenshots doesn’t provide some variable states. one question I wonder,
is it possible your ValueTree is empty?

a.
createXml() can return nullptr! (see documentation).
(I don’t see the state of xml or even checkup if it is initialised.

b.
I don’t understand why you’re actually inheriting it just to create an internal one…
as @oxxyyd also asked.

I don’t see where is globalDataTree object so we don’t know much about it.

To be honest, ValueTree isn’t super-straight-forward mechanism.
But once you understand the basic logic it can be really nice and with some careful reading can also be concurrent friendly.

The biggest usage tip for it is DBG(yourValueTree.toXmlString()); I’d actually create a new basic JUCE app from the Jucer, and try to add remove children and properties until you get the hang of it.

After that stage the second interesting property of it is the getting elements as Values and using yourValue.referTo(anotherValue); or getting a var pointer.

It has some really cool tricks.


#4

Never inherit from ValueTree!!

(Actually, that’s a good wake-up call for us - now that everything is C++11, we really should mark that class (and others!) as final to stop people making that kind of blunder)


#5

My bad, I renamed some variables and forgot to change others before I took the screenshot! I create a global ValueTree named globalDataTree and in the class constructor I equal it to ValueTree “UserSettings”. Attempting to use DBG (globalDataTree.toXmlString()) like you suggested however also causes a bad access as it appears the tree is empty (returns NULL). Any suggestions on where to go?


#6

A ValueTree created with the default constructor is invalid, see the docs:

ValueTree::ValueTree ( )
Creates an empty, invalid ValueTree.
A ValueTree that is created with this constructor can’t actually be used for anything, it’s just a default ‘null’ ValueTree that can be returned to indicate some sort of failure. To create a real one, use the constructor that takes a string.

Maybe that is the culprit…?


#7

Solved it! The pointer to the globalValueTree was scoped and so was being destructed and causing problems


#8

You should never either inherit from ValueTree, or have a pointer to one! It’s a by-value class. Use it like you would a String or var or something.

If you pull the latest develop, the class is now marked final so you can’t inherit, but sadly it’s not as easy to stop anyone allocating one on the heap!


#9

Just finished pulling it (thanks Tower for auto-fetch!) to guarantee this doesn’t happen in the future. Thanks again Jules!