views:

73

answers:

2

Here is source code of my function:

bool FieldModel::updateNode(QDomNode &node,const QString &parent){

  QDomElement rootOfTag;

  rootOfTag=fieldState.firstChild().firstChildElement(parent);

  qDebug()<<"Before"<<fieldState.toString();
  QDomNodeList sameTags=rootOfTag.elementsByTagName(node.firstChild().toElement().tagName());
  for(uint i=0;i<sameTags.length();i++){
      QDomNode nodeToReplace=sameTags.item(i);
      if(nodeToReplace.toElement().attribute("id")==node.firstChild().toElement().attribute("id")){
        nodeToReplace.parentNode().replaceChild(node,nodeToReplace);//Cause Memory Leak
        qDebug()<<"After"<<fieldState.toString();
        return true;
      }
  }

insertNode(node,parent);
return true;
}

Memory usage of this my program strictly increases, but when I removed line nodeToReplace.parentNode().replaceChild(node,nodeToReplace);, program uses stable amount of memory. I monitored fieldState(QDomDocument), and it is not growing while I'm using replaceChild (I make small changes). What can be problem?

Thanks.

+1  A: 

The docs say the call returns a reference to the old, replaced node on success:

Returns a new reference to oldChild on success or a null node an failure.

I don't know QT well but I'd guess it assumes you'll clean the old node reference up?

Rup
No, there is no memory management needed using these classes. If there is memory leaked, that's a bug in QDom.
Frank
@Frank: If `replaceChild` returns a reference to the old child then the memory clearly isn't deleted at that point. I see the bit in the [`QDomDocument`](http://doc.trolltech.com/4.6/qdomdocument.html#details) that says that memory for a node is destroyed when the last reference _and_ the dom document both go away. So it seems that until the `QDomDocument` is destroyed then old nodes will still be kicking around (even there are no more references to them).
Troubadour
Frank
By referencing Troubadour words, I tried fieldstate.setContent(fieldState.toString()), and it solved my problem :), but it is very inefficient as it seems :(
metdos
+3  A: 

Looks like a QDom bug to me (which wouldn't surprise me too much, just submitted a patch to make "<a:>foo</a:>" not crash QDom). Try with a minimal example. If that still leaks and if are on Linux or OS X, you could run it in valgrind. Both the example and valgrind output would make a good bug report.

Edit: Would be interesting if removeChild() also leaks for you

Frank