Troubles with saving ValueTree data

I think I must be misunderstanding something about ValueTree handling. I cant seem to get data saved to a file back into my ValueTree.

I have a method that saves the some settings in a ValueTree:

void DrumController::setFileForActivePad(const File file )
{
    synthAudioSource->setSampleForSound(lastSelectedPad - 1, file);

    String nodeName;
    nodeName << "pad" << lastSelectedPad;
    Identifier nodeIdentifier(nodeName);
    
    ValueTree padParameters = drumSettings.getOrCreateChildWithName(nodeIdentifier, nullptr);
    padParameters.setProperty("audioFilePath", file.getFullPathName(), nullptr);
    saveDefaultSettings();
    
}

this calls saveDefaultSettings which writes the ValueTree to a binary file:

void DrumController::saveSettings(File &settingsFile)
{
    std::cout << drumSettings.toXmlString();
    FileOutputStream os(settingsFile);
    drumSettings.writeToStream(os);

}

the console is showing me the contents of my ValueTree before writing to the file. For example:

<?xml version="1.0" encoding="UTF-8"?>

<drumSettings>
  <pad1 audioFilePath="/Users/alexgustafson/Music/newSample/perc/kick2.wav"/>
  <pad2 audioFilePath="/Users/alexgustafson/Music/newSample/perc/snare2.wav"/>
  <pad3 audioFilePath="/Users/alexgustafson/Music/newSample/perc/hihat_01.wav"/>
</drumSettings>

I can see the file size increasing as I write new data.
However when I import the file back to ValueTree I only see the first 2 nodes:

<?xml version="1.0" encoding="UTF-8"?>

<drumSettings>
  <pad1 audioFilePath="/Users/alexgustafson/Music/newSample/perc/kick2.wav"/>
</drumSettings>

This is the code for reading the data back into a ValueTree:

void DrumController::loadSettings(File &settingsFile)
{
    if (!settingsFile.exists()) {
        return;
    }
    
    FileInputStream is(settingsFile);
    drumSettings =   ValueTree::readFromStream(is);

    std::cout << drumSettings.toXmlString();
}

On my console I see:

<?xml version="1.0" encoding="UTF-8"?>

<drumSettings>
  <pad1 audioFilePath="/Users/alexgustafson/Music/newSample/perc/kick2.wav"/>
</drumSettings>

only the first child node exists. The others are missing.

Can anyone tell my what I’m doing wrong? Any tips would be greatly appreciated.

Thanks in advance!

Very odd - can’t see any obvious mistakes…

The fact that you have a pointer to a ValueTree is a bit strange though - they’re not the kind of object where you should ever need a pointer to one, and should never allocate one on the heap. So maybe the problem lies somewhere in the other code that you’ve not shown here.

I updated the code so it’s not using pointers. Same problem, unfortunately. ( I updated the post above to reflect the changes )

Do I need to close the IO Streams? It didn’t look like that was necessary.

I’m not using the drumSettings value tree in any other parts of the code, nor is saveSettings() being called, so it can’t be saving another version accidentally.

Any other tips?

Oh - I bet you’re appending to your existing file. Try deleting the file before writing to it.

Yes! That was it. Thank you maestro!

the fix:

void DrumController::saveSettings(File &settingsFile)
{
    settingsFile.deleteFile();
    FileOutputStream os(settingsFile);
    drumSettings.writeToStream(os);

}