由于某些原因,我正在编写QT程序。在一段代码中,有时我会获得双重免费或损坏错误,但不是100%的情况。例如,我按下按钮时会执行此代码:
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是一个QWidget,它在内部创建4个QWidget指针,所有指针都是mnrmanothprods作为父级:
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;
}
moduleSubScreen类仅将mnrmanothprods添加到布局中:
void moduleSubScreen::loadSubScreen(impgenmaint *child)
{
m_child = child;
connect(m_child,SIGNAL(closeCalled()),this,SLOT(close()));
ui->MainLayout->addWidget(child);
}
我将调试信息放在我的类的每个析构函数中,所以在关闭对话框后我通常会得到:
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,随机,有时我只得到部分调试的双删除错误:
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
知道如何才能捕捉到这个错误...或者为什么这样的错误不会一直发生?
感谢。 卡洛斯