Results 1 to 7 of 7

Thread: Memory leaks when read/write to DOM

  1. #1
    Join Date
    Mar 2008
    Posts
    57
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default 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?

    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 

  2. #2
    Join Date
    Mar 2008
    Posts
    57
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Memory leaks when read/write to DOM

    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.

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Memory leaks when read/write to DOM

    Quote Originally Posted by SailinShoes View Post
    I have to new
    Why would that be?
    J-P Nurmi

  4. #4
    Join Date
    Mar 2008
    Posts
    57
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default 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?

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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?
    J-P Nurmi

  6. #6
    Join Date
    Mar 2008
    Posts
    57
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default 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?

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Memory leaks when read/write to DOM

    Quote Originally Posted by SailinShoes View Post
    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).
    J-P Nurmi

Similar Threads

  1. memory leaks
    By Fastman in forum Qt Programming
    Replies: 1
    Last Post: 5th March 2008, 08:00
  2. Memory leak weirdness
    By Darhuuk in forum General Programming
    Replies: 10
    Last Post: 10th January 2008, 18:51
  3. Memory Leak in my Application :-(
    By Svaths in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2007, 19:42
  4. why there are memory leaks in Qt?
    By cocalele in forum Qt Programming
    Replies: 1
    Last Post: 19th March 2006, 09:55

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.