【问题标题】:Random double free or corruption error随机双重释放或损坏错误
【发布时间】:2011-11-18 00:59:46
【问题描述】:

出于某种原因,我正在使用 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

但是,有时我只是在部分调试中得到双重删除错误:

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

知道我该怎么做才能捕捉到这个错误......或者为什么这样的错误不会一直发生?

谢谢。 卡洛斯

【问题讨论】:

标签: qt memory-management


【解决方案1】:

您可以使用工具来提供帮助。像Valgrind 这样的东西通常很擅长找到这种东西。

【讨论】:

  • 嗨,我用 valgrind --leak-check=full --log-file=./mleaks.log ./myprogram 运行它...但除了 2 个小错误,我没有收到任何重大错误不相关的代码部分中的内存泄漏....虽然 Valgrind 无法跟踪它,所以我在我通常遇到崩溃的同一行中创建了一个控制问题,而 Valgrind 显示了受控问题...现在..我我瞎了! :-(
猜你喜欢
  • 2013-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多