Storing an Array<var> in ValueTree - what am i doing wrong?

So, i’m trying to store and Array in my plugin state.

Here’s what i’ve been doing so far:

void MyAudioProcessor::getStateInformation (MemoryBlock& data)
    Array<var> assignedSources;
    for( auto src : getAssignedSources() ) { // a std::unordered_map<size_t, string>
        size_t id = src.first;
        assignedSources.add((int64) id);
    if(!assignedSources.isEmpty()) {  
        parameters.state.setProperty(assignedSourcesID, assignedSources, nullptr);

    auto state = parameters.copyState();
    std::unique_ptr<XmlElement> xml (state.createXml()); // here it fails
    copyXmlToBinary (*xml, data);

In the marked line, i get an EXC_I386_BPT.

I don’t quite get why, the Array is valid, and the ValueTree class allows for adding arrays … so what am i doing wrong here ?


Ok, just noticing that arrays can be stored, but not exported as XML (?). Even though it has been a recommendation here:

So, what’d be the way to go here ?

Is it too late for you to store the ValueTree as binary data (using writeToStream) into your plugin’s state? (I don’t even understand what benefit the conversion to XML would even have…The host stores the plugin data as binary anyway.)

You can export it to an XML by converting the var array to a delimited string. Take a look at the ValueWithDefault::varArrayToDelimitedString() and ::delimitedStringToVarArray() methods in juce_data_structures.

Hmm, not really … it’s just the way it’s done in the Tutorial, so i stuck with it.

Yeah, I don’t understand why the tutorials do the seemingly unnecessary conversion into XML…(Since XML doesn’t even support the same data types directly as ValueTree.) :thinking: @jules

Hmm, when use writeToStream(), the writing goes well, but when i try to retrieve the data, i get an error because the data is NULL …

Ok, overlooked the static flag … got it now …

parameters.replaceState (ValueTree::readFromStream(istream));