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?

Qt Code:
  1. QDomDocument* DomManager::writeDom(MapContainer<QString,QString> outputMap)
  2. {
  3.  
  4. int writeCounter = 0;
  5.  
  6. QDomDocument *writeDomDoc = new QDomDocument(CommonDomProtocol::QSTR_HMI);
  7.  
  8. QDomElement root = writeDomDoc->createElement(CommonDomProtocol::QSTR_DOC);
  9. writeDomDoc->appendChild(root);
  10.  
  11. QDomElement elementAction = writeDomDoc->createElement(CommonDomProtocol::QSTR_API);
  12. elementAction.setAttribute(CommonDomProtocol::QSTR_ACTION,
  13. outputMap[CommonDomProtocol::QSTR_ACTION]);
  14. root.appendChild(elementAction);
  15.  
  16. QMapIterator<QString,QString> i(outputMap.getMap());
  17. while (i.hasNext())
  18. {
  19.  
  20. i.next();
  21.  
  22. if(i.key() != CommonDomProtocol::QSTR_ACTION)
  23. {
  24.  
  25. writeCounter += 1;
  26. QString toStr;
  27. toStr.setNum(writeCounter);
  28. QString str = CommonDomProtocol::QSTR_PARAM;
  29.  
  30. str.append(toStr);
  31. QDomElement elementParam = writeDomDoc->createElement(str);
  32.  
  33. elementParam.setAttribute(CommonDomProtocol::QSTR_TYPE, i.key());
  34. elementParam.setAttribute(CommonDomProtocol::QSTR_VALUE, i.value());
  35.  
  36. root.appendChild(elementParam);
  37.  
  38. }
  39.  
  40. }
  41.  
  42. return writeDomDoc;
  43.  
  44. }
  45.  
  46. MapContainer<QString,QString> DomManager::readDom(QDomDocument *readDomDoc)
  47. {
  48.  
  49. int readCounter = 1;
  50.  
  51. inputMap.removeAllItems(); // CLEAR THE MAP
  52.  
  53. QDomElement docElem = readDomDoc->documentElement();
  54. QDomNode node = docElem.firstChild();
  55.  
  56. while(!node.isNull())
  57. {
  58.  
  59. QString toStr;
  60. toStr.setNum(readCounter);
  61. QString str = CommonDomProtocol::QSTR_PARAM;
  62. str.append(toStr);
  63.  
  64. QDomElement element = node.toElement();
  65.  
  66. if(!element.isNull())
  67. {
  68.  
  69. if(element.tagName() == CommonDomProtocol::QSTR_API)
  70. {
  71.  
  72. inputMap.addTo(CommonDomProtocol::QSTR_ACTION,
  73. element.attribute(CommonDomProtocol::QSTR_ACTION,
  74. CommonDomProtocol::QSTR_GET_ATTRIBUTE));
  75.  
  76. }
  77. else if(element.tagName() == str)
  78. {
  79.  
  80. inputMap.addTo(element.attribute(CommonDomProtocol::QSTR_TYPE,
  81. CommonDomProtocol::QSTR_GET_ATTRIBUTE),
  82. element.attribute(CommonDomProtocol::QSTR_VALUE,
  83. CommonDomProtocol::QSTR_GET_ATTRIBUTE));
  84.  
  85. readCounter += 1;
  86.  
  87. }
  88.  
  89. }
  90.  
  91. node = node.nextSibling();
  92. }
  93.  
  94. delete readDomDoc;
  95.  
  96. return inputMap;
  97.  
  98. }
To copy to clipboard, switch view to plain text mode