PDA

View Full Version : Memory leaks when read/write to DOM



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;

}

SailinShoes
20th March 2008, 09:59
The QDomDocument Class Reference says...


The parsed XML is represented internally by a tree of objects that can be accessed using the various QDom classes. All QDom classes only reference objects in the internal tree. The internal objects in the DOM tree will get deleted once the last QDom object referencing them and the QDomDocument itself are deleted.


I have to new...and delete not only the QDomDocument but QDomElements (QDom objects referencing them) to.

jpn
20th March 2008, 10:56
I have to new
Why would that be?

SailinShoes
20th March 2008, 11:14
Something in the above read/write to DOMcode is not correct, because it leaks memory.

Im a bit uncertain? But to be sure that not only the QDomDocument gets deleted when it is not needed anymore, I have to be sure that the QDomElements is removed out of memory to?

jpn
20th March 2008, 11:22
What I was trying to ask is that why do you need to allocate any of those DOM objects on the heap in the first place?

SailinShoes
20th March 2008, 11:33
I first started by not allocate anything on the heap. But it leaked memory.

So I have started to try to allocate on the heap in order to be able delete when I dont want the object anymore. It still leaked memory.

So I figured, if I allocate the element objects to, and delete them when I dont want them anymore.

I very much would like to not allocate anything on the heap, but am not sure how & were the objects goes out of scope?

jpn
20th March 2008, 11:51
I first started by not allocate anything on the heap. But it leaked memory.
How did you verify this?


I very much would like to not allocate anything on the heap, but am not sure how & were the objects goes out of scope?
Well, that's something fundamental you just have to know about the programming language you use before you even start working with a toolkit like Qt. I suggest you refer to your favourite C++ book (notice that there are free and online books out there, you might want to search the forums for those).