Parsing XML file ends with LinkedListPointer error


#1

Dear all, I cannot succeed in parsing a XML file containing presets, I'm using scopedPointer, it parses properly and once it returns from the parsing function, I get an error in the LinkedListPointer class, error that I cannot understand.

​My Xml :

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

<Preset>
  <Info Date="10 Feb 2016 2:59:16pm" AppVer="2.5"/>
  <Device prodId="4" FwVer="2.4" BlVer="2.1"/>
  <Params>
    <Reg Reg52="00000004"/>
    <Reg Reg53="00000034"/>
  </Params>
</Preset>

My parsing function :

int MyClass::loadPresetFromFile(File theFile)
{
    u32 parametersArrayFromXml[32];
    ScopedPointer<XmlElement> mainElement = XmlDocument::parse(theFile);
    ScopedPointer<XmlElement> currentElement;
    String wordStr;
    String paramIdFromXmlStr;
    String paramValueFromXmlStr;  

    if (mainElement->hasTagName (TRANS("Preset")))
    {
        // parse header and check if this preset file is compatible
        currentElement = mainElement->getFirstChildElement();

        if(currentElement->hasTagName(TRANS("Info")))
        {
            currentElement = currentElement->getNextElementWithTagName(TRANS("Device"));
            if(currentElement != nullptr)
            {
                if(currentElement->hasAttribute(TRANS("prodId")))
                {
                    u8 prodIdFromXml = currentElement->getIntAttribute(TRANS("prodId"));
                    if(prodIdFromXml == midiController->SYSX_PRODUCT_ID_25_37)
                    {
                        // parse all preset file and update UI + connected device
                        currentElement = currentElement->getNextElement();
                        if(currentElement != nullptr)
                        {
                            if(currentElement->hasTagName(TRANS("Params")))
                            {                            
                                // grab a ref to midiController class
                                MidiController *midiController = MidiController::getInstance();
                                // parse all params
                                forEachXmlChildElementWithTagName(*currentElement, registerElement, TRANS("Reg"))
                                {

                                    paramIdFromXmlStr = registerElement->getAttributeName(0);
                                    paramValueFromXmlStr = registerElement->getAttributeValue(0);
                                    // extract values from strings
                                    u8 paramIdFromXml = (u8) paramIdFromXmlStr.getTrailingIntValue();
                                    u8 nbWords = paramsWordsLenghtTable[paramIdFromXml];
                                    if(paramValueFromXmlStr.length() != (nbWords*8)) return PRESET_FILE_PARAM_SIZE_CORRUPTED;
                                    int offsetInStr = 0;
                                    for(int j=0; j<nbWords; j++)
                                    {
                                        wordStr = paramValueFromXmlStr.substring(offsetInStr, offsetInStr+8);
                                        parametersArrayFromXml[j] = wordStr.getHexValue32();
                                        offsetInStr += 8;
                                    }

                                    midiController->paramId = paramIdFromXml;
                                    midiController->readSingleRegisterCallback(ACK_SUCCESS, &parametersArrayFromXml[0], false);                              
                                }
                            }
                            else
                                return PRESET_FILE_TAG_ELEMENT_NOT_FOUND;
                        }
                        else
                            return PRESET_FILE_TAG_ELEMENT_NOT_FOUND;
                    }
                    else
                        return PRESET_FILE_WRONG_PRODUCT_ID;
                }
                else
                    return PRESET_FILE_TAG_ELEMENT_NOT_FOUND;
            }
            else
                return PRESET_FILE_TAG_ELEMENT_NOT_FOUND;
        }
        else
            return PRESET_FILE_TAG_ELEMENT_NOT_FOUND;
    }
    else
        return PRESET_FILE_MAIN_ELEMENT_NOT_FOUND;   

    return PRESET_FILE_OK;
}

Any help would be greatly appreciated, I'm stuck on this for 2 days!
Thanks
Jerome


#2

"currentElement" should be a regular pointer, not a ScopedPointer. All the child nodes in a XML structure are owned by their parent. This means recursive deallocation when you deallocate the root node.

If you point a scopedpointer to some child node, the moment you set it to something else, you actually deallocate the previous node and that leaves its parent with a dangling pointer.


#3

Ahhhhh thanks, you made my day !
it works now !