Memory leaks when read/write to DOM
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?
Code:
QDomDocument* DomManager
::writeDom(MapContainer<QString,QString> outputMap
) {
int writeCounter = 0;
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;
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
while(!node.isNull())
{
toStr.setNum(readCounter);
QString str
= CommonDomProtocol
::QSTR_PARAM;
str.append(toStr);
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;
}
Re: Memory leaks when read/write to DOM
The QDomDocument Class Reference says...
Quote:
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.
Re: Memory leaks when read/write to DOM
Quote:
Originally Posted by
SailinShoes
I have to new
Why would that be?
Re: Memory leaks when read/write to DOM
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?
Re: Memory leaks when read/write to DOM
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?
Re: Memory leaks when read/write to DOM
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?
Re: Memory leaks when read/write to DOM
Quote:
Originally Posted by
SailinShoes
I first started by not allocate anything on the heap. But it leaked memory.
How did you verify this?
Quote:
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).