Results 1 to 3 of 3

Thread: QMap deleting

  1. #1
    Join Date
    May 2012
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2

    Default QMap deleting

    I would like to have QMap as a part of class, so I add pointer to QMap in class private section, invoke a constructor of QMap in class constructor, and invoke destructor of QMap in class destructor. But if I invoke destructor of QMap, Segmentation fault is signaled. What do I wrong?

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QMap>
    3. #include <QMutableMapIterator>
    4.  
    5. class Segment
    6. {
    7. public:
    8. Segment(quint32 sourceIP, quint32 destinationIP,
    9. quint32 sequenceNumber,const unsigned char *segment)
    10. {
    11. qDebug("%s",Q_FUNC_INFO);
    12. srcIP=sourceIP;
    13. dstIP=destinationIP;
    14. seqNo=sequenceNumber;
    15. data=segment;
    16. };
    17. ~Segment()
    18. {
    19. qDebug("%s",Q_FUNC_INFO);
    20. };
    21. quint32 srcIP;
    22. quint32 dstIP;
    23. quint32 seqNo;
    24. private:
    25. const unsigned char *data;
    26. };
    27.  
    28. class Connection
    29. {
    30. public:
    31. Connection(quint32 sourceIP, quint32 destinationIP)
    32. {
    33. qDebug("%s",Q_FUNC_INFO);
    34. srcIP= sourceIP;
    35. dstIP= destinationIP;
    36. dstRCVMap= new QMap<quint32, Segment*>;
    37. };
    38. ~Connection()
    39. {
    40. qDebug("%s",Q_FUNC_INFO);
    41. QMutableMapIterator<quint32, Segment*> mapIter2(*dstRCVMap);
    42. while (mapIter2.hasNext()){
    43. Segment *segment= mapIter2.next().value();
    44. delete segment;
    45. mapIter2.remove();
    46. };
    47. delete dstRCVMap;//Problem
    48. qDebug("END OF %s",Q_FUNC_INFO);
    49. };
    50. void append(Segment *segment)
    51. {
    52. qDebug("%s",Q_FUNC_INFO);
    53. if (segment->srcIP==srcIP && segment->dstIP==dstIP)
    54. dstRCVMap->insert(segment->seqNo, segment);
    55. else
    56. delete segment;
    57. };
    58. private:
    59. quint32 srcIP;
    60. quint32 dstIP;
    61. QMap<quint32, Segment*> *dstRCVMap;
    62. };
    63.  
    64. int main(int argc, char *argv[])
    65. {
    66. QCoreApplication a(argc, argv);
    67. quint32 fakeIP1=123;
    68. quint32 fakeIP2=456;
    69. Connection con(fakeIP1,fakeIP2);
    70. const unsigned char rawSegment[20]={0};
    71. quint32 fakeSeqNo=1;
    72. con.append(new Segment(fakeIP1,fakeIP2,fakeSeqNo,rawSegment));
    73. // return a.exec();
    74. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2011
    Posts
    354
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows
    Thanks
    17
    Thanked 27 Times in 24 Posts

    Default Re: QMap deleting

    On map destruction all iterators become invalid. In your code, being invalid, iterator is accessed again (released) when exiting from the destructor (iterator is allocated on the stack). This causes the issue. Allocate both map and iterator on the stack or both on the heap.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: QMap deleting

    The problem comes because the mutable iterator is holding a pointer or reference to the map. You delete the map and then, as the iterator destructor runs it crashes. You have two options:
    1. Use a const iterator and do not remove the entries after deleting the segments. There's little point removing them at this point anyway: the map destructor will soon do it.
    2. Use qDeleteAll(*dstRCVMap), which has the same effect as option 1.
    3. Wrap the QMutableMapIterator loop in an anonymous block to make sure the iterator is destroyed (by leaving scope) before the map.

    Ok, that's three options, but the third is ugly for most definitions of ugly.

  4. The following user says thank you to ChrisW67 for this useful post:

    morfis (12th June 2012)

Similar Threads

  1. QMap
    By sophister in forum Qt Programming
    Replies: 5
    Last Post: 25th May 2009, 11:05
  2. Deleting QProcess
    By user_mail07 in forum Qt Programming
    Replies: 7
    Last Post: 29th January 2008, 19:55
  3. QMap
    By AnithaRagupathy in forum Qt Programming
    Replies: 5
    Last Post: 23rd November 2007, 14:26
  4. Qmap
    By phillip_Qt in forum Qt Programming
    Replies: 3
    Last Post: 23rd November 2007, 11:43
  5. Reg - QMap(Qt3.3.4)
    By suresh in forum Newbie
    Replies: 3
    Last Post: 18th October 2006, 23:04

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
  •  
Qt is a trademark of The Qt Company.