Results 1 to 4 of 4

Thread: Getting random double free or corruption in the same line of code!

  1. #1
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    24
    Thanked 17 Times in 14 Posts

    Unhappy Getting random double free or corruption in the same line of code!

    Hi,

    I am writing a program with QT and for some reason in a piece of code sometimes I get double free or corruption error but not in 100% of the cases, I would say I get it 3 times a week. The code that produce the error is the following:

    Qt Code:
    1. void manureman::on_BitBtn3_clicked()
    2. {
    3. mnrmanothprods *otherprods = new mnrmanothprods(this,database,currentSystem); //Create the subWindow
    4. moduleSubScreen m_dialogWindow; //Create the dialog for the subwindow
    5. m_dialogWindow.loadSubScreen(otherprods); //Add the subwindow to the dialog... Basilly a MainLayout->addWidget
    6. m_dialogWindow.setWindowTitle("Purchased products for manure management");
    7. qDebug() << "Loading window";
    8. m_dialogWindow.exec();
    9. qDebug() << "After loading window";
    10.  
    11. qDebug() << "Exiting clicked";
    12. }
    To copy to clipboard, switch view to plain text mode 

    mnrmanothprods is a QWidget descendant class that create internally 4 QWidget pointers all with mnrmanothprods as parent:

    Qt Code:
    1. void mnrmanothprods::loadForm()
    2. {
    3. ..
    4. m_colModel = new fieldInColModel(this);
    5. ...
    6. m_periodModel = new periodTableModel(this);
    7. ...
    8. }
    9.  
    10. QAbstractItemDelegate* mnrmanothprods::constructCustomDelegator(QString, QString field)
    11. {
    12. if (field == "COLLECTED")
    13. {
    14. imageCheckDelegate *ckhdelegate = new imageCheckDelegate(this);
    15. ckhdelegate->setCheckPixMap(QPixmap(":/images/ok.png"));
    16. ckhdelegate->setUnCheckPixMap(QPixmap(":/images/nocheck.png"));
    17. return ckhdelegate;
    18. }
    19. if (field == "ava")
    20. {
    21. imageCheckDelegate *ckhdelegate = new imageCheckDelegate(this);
    22. ckhdelegate->setCheckPixMap(QPixmap(":/images/ok.png"));
    23. ckhdelegate->setUnCheckPixMap(QPixmap(":/images/nocheck.png"));
    24. ckhdelegate->addIgnoredColumn(0);
    25. return ckhdelegate;
    26. }
    27. return 0;
    28. }
    To copy to clipboard, switch view to plain text mode 

    The moduleSubScreen class only adds mnrmanothprods to a layout with:

    Qt Code:
    1. void moduleSubScreen::loadSubScreen(impgenmaint *child)
    2. {
    3. m_child = child;
    4. connect(m_child,SIGNAL(closeCalled()),this,SLOT(close()));
    5. ui->MainLayout->addWidget(child);
    6. }
    To copy to clipboard, switch view to plain text mode 

    I put debug info in each destructor of my classes so I can see what is getting destroyed and the order. For example after I close the dialog I normallly get:

    Qt Code:
    1. Debug: After loading window
    2. Debug: Exiting clicked
    3. Debug: Before destroy moduleSubScreen UI
    4. Debug: After destroy moduleSubScreen UI
    5. Debug: After m_child = 0
    6. Debug: Before destroy mnrmanothprods UI
    7. Debug: After destroy mnrmanothprods UI
    8. Debug: Destroy: fieldInColModel
    9. Debug: Destroy imageCheckDelegate
    10. Debug: Destroy: periodTableModel
    11. Debug: Destroy imageCheckDelegate
    To copy to clipboard, switch view to plain text mode 

    However when it crashes I get a partial destroy output:
    Qt Code:
    1. Debug: After loading window
    2. Debug: Exiting clicked
    3. Debug: Before destroy moduleSubScreen UI
    4. Debug: After destroy moduleSubScreen UI
    5. Debug: After m_child = 0
    6. Debug: Before destroy mnrmanothprods UI
    7. Debug: After destroy mnrmanothprods UI
    8. Debug: Destroy: fieldInColModel
    To copy to clipboard, switch view to plain text mode 

    Seem likes it fails after Destroy: fieldInColModel as it does not reach Destroy imageCheckDelegate

    I ran it with valgrind --leak-check=full --log-file=./mleaks.log ./myprogram ... but I did not get any significant error besides 2 minor memory leaks in an unrelated section of code.... I though Valgrind was not able to trace it so I created a control problem in the same lines where I usually get the crash and Valgrind showed the controlled problem...

    I tried to dump some info with this->dumpObjectInfo();
    this->dumpObjectTree(); but even though my program is not on release I don't get any output from those dumps.

    Any idea what else can I check?

    Many thanks,
    Carlos.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanked 342 Times in 324 Posts

    Default Re: Getting random double free or corruption in the same line of code!

    Build a debug version and use debugger, for example gdb - after the crash you can examine the call stack, check the value of each variable, even call methods on objects. Read more here: Debugging with gdb.
    After you addWidget() to layout, be sure you dont try to delete it yourself, because The ownership of item is transferred to the layout, and it's the layout's responsibility to delete it.
    Another thing, don't store pointers to QObjects, QWidgets in C-style ( by QObject * m_object, I assume the m_child is declared as QWidget * ), use QPointer, its much safer - it will be NULL-ed automatically when the objects is deleted, so the possibility of double-deletion is reduced.

  3. #3
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    24
    Thanked 17 Times in 14 Posts

    Default Re: Getting random double free or corruption in the same line of code!

    QTCreator give me: &"warning: GDB: Failed to set controlling terminal: Invalid argument\n"

    Is this fine or I may change something?

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanked 342 Times in 324 Posts

    Default Re: Getting random double free or corruption in the same line of code!

    I don't know, can you run it from the command line (gdb "path/to/app.exe") ?

  5. The following user says thank you to stampede for this useful post:

    qlands (16th September 2011)

Similar Threads

  1. Replies: 1
    Last Post: 7th April 2010, 16:26
  2. Replies: 0
    Last Post: 29th September 2009, 01:16
  3. Replies: 3
    Last Post: 5th June 2009, 19:19
  4. Free code coverage analyzers
    By jpn in forum General Programming
    Replies: 4
    Last Post: 30th August 2006, 17:29

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.