SailinShoes
20th March 2008, 08:50
I shuffle data between two components in an application using DOM. The sender component writes data to a DOM tree in memory and emits the reference. The receving component gets the reference and use it to read data from the DOM tree.
I get memory leaks when I read/write data to DOM in memory. I'll post the code and hopefully someone can spot something that could cause the problem?
Could the problem be that I only delete the pointer to the DOM element? Do I need to 'new' the elements as well, and delete them to?
QDomDocument* DomManager::writeDom(MapContainer<QString,QString> outputMap)
{
int writeCounter = 0;
QDomDocument *writeDomDoc = new QDomDocument(CommonDomProtocol::QSTR_HMI);
QDomElement root = writeDomDoc->createElement(CommonDomProtocol::QSTR_DOC);
writeDomDoc->appendChild(root);
QDomElement elementAction = writeDomDoc->createElement(CommonDomProtocol::QSTR_API);
elementAction.setAttribute(CommonDomProtocol::QSTR _ACTION,
outputMap[CommonDomProtocol::QSTR_ACTION]);
root.appendChild(elementAction);
QMapIterator<QString,QString> i(outputMap.getMap());
while (i.hasNext())
{
i.next();
if(i.key() != CommonDomProtocol::QSTR_ACTION)
{
writeCounter += 1;
QString toStr;
toStr.setNum(writeCounter);
QString str = CommonDomProtocol::QSTR_PARAM;
str.append(toStr);
QDomElement elementParam = writeDomDoc->createElement(str);
elementParam.setAttribute(CommonDomProtocol::QSTR_ TYPE, i.key());
elementParam.setAttribute(CommonDomProtocol::QSTR_ VALUE, i.value());
root.appendChild(elementParam);
}
}
return writeDomDoc;
}
MapContainer<QString,QString> DomManager::readDom(QDomDocument *readDomDoc)
{
int readCounter = 1;
inputMap.removeAllItems(); // CLEAR THE MAP
QDomElement docElem = readDomDoc->documentElement();
QDomNode node = docElem.firstChild();
while(!node.isNull())
{
QString toStr;
toStr.setNum(readCounter);
QString str = CommonDomProtocol::QSTR_PARAM;
str.append(toStr);
QDomElement element = node.toElement();
if(!element.isNull())
{
if(element.tagName() == CommonDomProtocol::QSTR_API)
{
inputMap.addTo(CommonDomProtocol::QSTR_ACTION,
element.attribute(CommonDomProtocol::QSTR_ACTION,
CommonDomProtocol::QSTR_GET_ATTRIBUTE));
}
else if(element.tagName() == str)
{
inputMap.addTo(element.attribute(CommonDomProtocol ::QSTR_TYPE,
CommonDomProtocol::QSTR_GET_ATTRIBUTE),
element.attribute(CommonDomProtocol::QSTR_VALUE,
CommonDomProtocol::QSTR_GET_ATTRIBUTE));
readCounter += 1;
}
}
node = node.nextSibling();
}
delete readDomDoc;
return inputMap;
}
I get memory leaks when I read/write data to DOM in memory. I'll post the code and hopefully someone can spot something that could cause the problem?
Could the problem be that I only delete the pointer to the DOM element? Do I need to 'new' the elements as well, and delete them to?
QDomDocument* DomManager::writeDom(MapContainer<QString,QString> outputMap)
{
int writeCounter = 0;
QDomDocument *writeDomDoc = new QDomDocument(CommonDomProtocol::QSTR_HMI);
QDomElement root = writeDomDoc->createElement(CommonDomProtocol::QSTR_DOC);
writeDomDoc->appendChild(root);
QDomElement elementAction = writeDomDoc->createElement(CommonDomProtocol::QSTR_API);
elementAction.setAttribute(CommonDomProtocol::QSTR _ACTION,
outputMap[CommonDomProtocol::QSTR_ACTION]);
root.appendChild(elementAction);
QMapIterator<QString,QString> i(outputMap.getMap());
while (i.hasNext())
{
i.next();
if(i.key() != CommonDomProtocol::QSTR_ACTION)
{
writeCounter += 1;
QString toStr;
toStr.setNum(writeCounter);
QString str = CommonDomProtocol::QSTR_PARAM;
str.append(toStr);
QDomElement elementParam = writeDomDoc->createElement(str);
elementParam.setAttribute(CommonDomProtocol::QSTR_ TYPE, i.key());
elementParam.setAttribute(CommonDomProtocol::QSTR_ VALUE, i.value());
root.appendChild(elementParam);
}
}
return writeDomDoc;
}
MapContainer<QString,QString> DomManager::readDom(QDomDocument *readDomDoc)
{
int readCounter = 1;
inputMap.removeAllItems(); // CLEAR THE MAP
QDomElement docElem = readDomDoc->documentElement();
QDomNode node = docElem.firstChild();
while(!node.isNull())
{
QString toStr;
toStr.setNum(readCounter);
QString str = CommonDomProtocol::QSTR_PARAM;
str.append(toStr);
QDomElement element = node.toElement();
if(!element.isNull())
{
if(element.tagName() == CommonDomProtocol::QSTR_API)
{
inputMap.addTo(CommonDomProtocol::QSTR_ACTION,
element.attribute(CommonDomProtocol::QSTR_ACTION,
CommonDomProtocol::QSTR_GET_ATTRIBUTE));
}
else if(element.tagName() == str)
{
inputMap.addTo(element.attribute(CommonDomProtocol ::QSTR_TYPE,
CommonDomProtocol::QSTR_GET_ATTRIBUTE),
element.attribute(CommonDomProtocol::QSTR_VALUE,
CommonDomProtocol::QSTR_GET_ATTRIBUTE));
readCounter += 1;
}
}
node = node.nextSibling();
}
delete readDomDoc;
return inputMap;
}