QLands
QLands

Reputation: 2586

Random double free or corruption error

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

Upvotes: 1

Views: 2285

Answers (1)

Jeff Foster
Jeff Foster

Reputation: 44706

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

Upvotes: 2

Related Questions