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


#1

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 ?

Best,
N


#2

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 ?


#3

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.)


#4

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.


#5

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


#6

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


#7

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 …


#8

Ok, overlooked the static flag … got it now …

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