XMLElement NULL

Hello guys,

I am new in JUCE and I found myself stuck when I am trying to load preset.

Whenever I am trying to load a preset I am having a message “Thread 1: EXC_BAD_ACCESS(code=EXC_I386_GPFTL)”, and that is on the LinkedListPointer& function. It shows that the juce::XMLElement::XMLAttributeNode* is NULL. Therefore I guess it cannot return the attributes.size.

The code I am using in the getStateInformation is:

` XmlElement preset (“KAP_StateInfo”);

XmlElement* presetBody = new XmlElement ("KAP_Preset");

mPresetManager->getXmlForPreset(presetBody);

preset.addChildElement(presetBody);

copyXmlToBinary(preset, destData);  `

I have also made a Preset manager class where I have all the function to get xml from preset or load and make new preset.

The getXmlForPreset function code is:

void KAPPresetManager::getXmlForPreset (XmlElement* inElement)

{
auto& parameters = mProcessor->getParameters();

for (int i = 0; i <parameters.size(); i++)
{
    AudioProcessorParameterWithID* parameter =
    (AudioProcessorParameterWithID*)parameters.getUnchecked(i);
    
    inElement->setAttribute(parameter->paramID,
                            parameter->getValue());
}

}

In my understanding everything is ok so far. Isn’t it?

Then the setStateInformation function code is:

    //XmlElement* xmlState = getXmlFromBinary(data, sizeInBytes).get(); 

std::unique_ptr<XmlElement> xmlState;
xmlState.reset(getXmlFromBinary(data, sizeInBytes).get());

if(xmlState.get() != nullptr)
{
    forEachXmlChildElement(*xmlState, subChild)
    {
        mPresetManager->loadPresetForXml(subChild); // (Thread 1: EXC_BAD_ACCESS(code=EXC_I386_GPFTL) )
    }
} else
    jassertfalse;

And the load preset for xml function code is:

void KAPPresetManager::loadPresetForXml (XmlElement* inElement)

{
mCurrentPresetXml = inElement;

auto& _parameters = mProcessor->getParameters();

for (int i = 0; i < mCurrentPresetXml->getNumAttributes(); i++) // (Thread 1: EXC_BAD_ACCESS(code=EXC_I386_GPFTL) )
{
    const String paramId = mCurrentPresetXml->getAttributeName(i);
    const float value = mCurrentPresetXml->getDoubleAttribute(paramId);
    
    for (int j = 0; j < _parameters.size(); j++)
    {
        AudioProcessorParameterWithID* parameter =
        (AudioProcessorParameterWithID*)_parameters.getUnchecked(i);
        
        if (paramId == parameter->paramID)
        {
            parameter->setValueNotifyingHost(value);
        }
    }
}

}

I cannot understand why is this happening and I cannot find a solution no matter what I have tried. Anyone can help I would really appriciate.

Looks like the Kadenze course code :sweat_smile:

Are you sure:

forEachXmlChildElement(*xmlState, subChild)
{
    mPresetManager->loadPresetForXml(subChild); // (Thread 1: EXC_BAD_ACCESS(code=EXC_I386_GPFTL) )
}

Is what you want?

It looks like the XML created by your function doesn’t have children.

Maybe it should just be?

if(xmlState.get() != nullptr)
{
mPresetManager->loadPresetForXml(xmlState);

Hey Jake,

Thank you for your reply.

You know it is the code from the Kadenze course :rofl:

I tried it but it did not work. It is still telling me that there are not child elements when the ~XMLElement tries to delete them.

Your code formatting was looking a bit strange sorry.

There is a memory mangement issue, as well as an accessing issue in your code.

you’re storing the XML binary provided by the host in a std::unique_ptr:

std::unique_ptr<XmlElement> xmlState;
xmlState.reset(getXmlFromBinary(data, sizeInBytes).get());

Then later, you’re iterating this XML element incorrectly and passing each small child element (if they exist) into the preset manager:

forEachXmlChildElement(*xmlState, subChild)
{
    mPresetManager->loadPresetForXml(subChild); // (Thread 1: EXC_BAD_ACCESS(code=EXC_I386_GPFTL) )
}

then in your presetmananger, you’re storing each child element, as if it is the actual preset file itself:

mCurrentPresetXml = inElement;

It looks like there’s a couple problems, but the first step, is wherever you’re truly getting bad access, is to place and breakpoint, and very what is or is not null & why.

Good luck!

Thank you Jake.

That is very helpful!