Problem with writing the valueTreeState into an xml file

I have this code:

// check:
int k = valueTreeState.getParameter("DETUNE")->convertFrom0to1(valueTreeState.getParameter("DETUNE")->getValue());

const auto xml = valueTreeState.copyState().createXml();
const auto presetFile = topDirectory.getChildFile(currentBankName).getChildFile(presetName + "." + extension);
if (!xml->writeTo(presetFile))
{
    DBG("Could not write preset file: " + presetFile.getFullPathName());
    jassertfalse;
}

When I run the debugger and look at the value of k, it is always correct, say -23.
However, the resulting xml file always shows the default value (and this is true for all other parameters…):

<PARAM id="DETUNE" value="0.0"/>

This parameter was defined as follows:

std::make_unique<juce::AudioParameterFloat>("DETUNE", "FM Detune", juce::NormalisableRange<float>{-40.f, 40.f, 1.f}, 0.f)

What could be the issue here?

I can’t see anything obviously wrong with that, so two dumb questions first. Are you sure you’re looking at the correct file on the disk? Is there any other code resetting values to default and writing to file before you inspect it?

Btw, if you haven’t seen it Reuben mentions a neat way of storing a reference to your parameter before you add it to the layout. Then you could just do k = detuneParamRef->get().

The date & time on the xml file always corresponds to when I saved it…
Nothing else resets values and/or writes to file…
:astonished:

Put in a DBG…

const auto xml = valueTreeState.copyState().createXml();

DBG (xml->toString());

Rail