1

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. For example, I have this code that is executed when I press a button:

void manureman::on_BitBtn3_clicked()
{
    mnrmanothprods *otherprods = new mnrmanothprods(this,database,currentSystem);
    moduleSubScreen m_dialogWindow;
    m_dialogWindow.loadSubScreen(otherprods);
    m_dialogWindow.setWindowTitle("Purchased products for manure management");
    qDebug() << "Loading window";
    m_dialogWindow.exec();
    qDebug() << "After loading window";

    qDebug() << "Exiting";
}

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

void mnrmanothprods::loadForm()
{
  m_colModel = new fieldInColModel(this);
  m_periodModel = new periodTableModel(this);
}

QAbstractItemDelegate* mnrmanothprods::constructCustomDelegator(QString, QString field)
{
    if (field == "COLLECTED")
    {
        imageCheckDelegate *ckhdelegate = new imageCheckDelegate(this);
        ckhdelegate->setCheckPixMap(QPixmap(":/images/ok.png"));
        ckhdelegate->setUnCheckPixMap(QPixmap(":/images/nocheck.png"));
        return ckhdelegate;
    }
    if (field == "ava")
    {
        imageCheckDelegate *ckhdelegate = new imageCheckDelegate(this);
        ckhdelegate->setCheckPixMap(QPixmap(":/images/ok.png"));
        ckhdelegate->setUnCheckPixMap(QPixmap(":/images/nocheck.png"));
        ckhdelegate->addIgnoredColumn(0);
        return ckhdelegate;
    }
    return 0;
}

The moduleSubScreen class only adds mnrmanothprods to a layout with:

    void moduleSubScreen::loadSubScreen(impgenmaint *child)
    {
        m_child = child;
        connect(m_child,SIGNAL(closeCalled()),this,SLOT(close()));
        ui->MainLayout->addWidget(child);
    } 

I put debug info in each destructor of my classes so after I close the dialog I normally get:

Debug: After loading window 
Debug: Exiting 
Debug: Before destroy moduleSubScreen UI 
Debug: After destroy moduleSubScreen UI 
Debug: After m_child = 0 
Debug: Before destroy mnrmanothprods UI 
Debug: After destroy mnrmanothprods UI 
Debug: Destroy: fieldInColModel 
Debug: Destroy imageCheckDelegate 
Debug: Destroy: periodTableModel 
Debug: Destroy imageCheckDelegate

Howerver, randomly, sometimes I get the double deletion error with just part of the debug:

Debug: After loading window 
    Debug: Exiting 
    Debug: Before destroy moduleSubScreen UI 
    Debug: After destroy moduleSubScreen UI 
    Debug: After m_child = 0 
    Debug: Before destroy mnrmanothprods UI 
    Debug: After destroy mnrmanothprods UI 
    Debug: Destroy: fieldInColModel

Any idea how what can I do to catch the bug... or Why a bug like this does not happen all the time?

Thanks. Carlos

Tom Zych
  • 13,329
  • 9
  • 36
  • 53
QLands
  • 2,424
  • 5
  • 30
  • 50
  • Possible duplicate of [How to track down a "double free or corruption" error](https://stackoverflow.com/questions/2902064/how-to-track-down-a-double-free-or-corruption-error) – Raedwald Dec 06 '18 at 13:52

1 Answers1

2

You could use a tool to help. Something like Valgrind is usually pretty good at finding this sort of thing.

Jeff Foster
  • 43,770
  • 11
  • 86
  • 103
  • Hi, 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... Now.. i am blind! :-( – QLands Sep 16 '11 at 06:01