Qt5在关闭时崩溃

时间:2014-06-16 18:18:29

标签: c++ qt segmentation-fault qt5

我的项目有时会在关闭时崩溃,但不可靠。当使用“取消”按钮关闭时,它比使用转义键关闭时更频繁地崩溃,这进一步让我感到困惑。我有一个简单的查找对话框(较大项目的一小部分)作为测试用例的一种形式。

尝试使用GDB进行调试会导致它永远不会崩溃。核心转储是垃圾(坏内存)。

以下是来源。我无法确定究竟出了什么问题;我已经看到(经过微小修改)双重释放,释放NULL,释放一个从未存在过的指针等等。我开始怀疑它是否是部分Qt的错误。

部首:

#ifndef NIDE_FINDDIALOG_HPP
#define NIDE_FINDDIALOG_HPP

#include <QDialog>
#include <QString>
#include <QGridLayout>
#include <QCheckBox>
#include <QLineEdit>
#include <QPushButton>
#include <QWidget>

class FindDialog: public QDialog {
public:
    bool regex, caseSensitive, inSelection, wholeWord, forward, wrap;
    QString expr;

private:
    struct {
        QGridLayout *layout;
        QCheckBox *regex, *caseSense, *select, *whole, *forward, *wrap;
        QLineEdit *exprBox;
        QPushButton *cancel, *find;
    }ui;

    void onCancel();
    void onFind();

    void onRegex();
    void onCase();
    void onSelect();
    void onWhole();
    void onForward();
    void onWrap();

public:
    FindDialog(QWidget *parent);
    ~FindDialog();
};

#endif/*NIDE_FINDDIALOG_HPP*/

来源:

#include <NIDE/FindDialog.hpp>

FindDialog::FindDialog(QWidget *parent): QDialog(parent),
    regex(false), caseSensitive(true), inSelection(false), wholeWord(true),
    forward(true), wrap(true), expr() {

    ui.layout       = new QGridLayout(this);    
    ui.regex        = new QCheckBox("Regex", this);
    ui.caseSense    = new QCheckBox("Match case", this);
    ui.select       = new QCheckBox("In Selection", this);
    ui.whole        = new QCheckBox("Whole Word", this);
    ui.forward      = new QCheckBox("Forward", this);
    ui.wrap         = new QCheckBox("Wrap around", this);
    ui.exprBox      = new QLineEdit(this);
    ui.cancel       = new QPushButton("Cancel", this);
    ui.find         = new QPushButton("Find", this);

    ui.regex->setChecked(regex);
    connect(ui.regex, &QCheckBox::stateChanged, this, &FindDialog::onRegex);

    ui.caseSense->setChecked(caseSensitive);
    connect(ui.caseSense, &QCheckBox::stateChanged, this, &FindDialog::onCase);

    ui.select->setChecked(inSelection);
    connect(ui.select, &QCheckBox::stateChanged, this, &FindDialog::onSelect);

    ui.whole->setChecked(wholeWord);
    connect(ui.whole, &QCheckBox::stateChanged, this, &FindDialog::onWhole);

    ui.forward->setChecked(forward);
    connect(ui.forward, &QCheckBox::stateChanged, this, &FindDialog::onForward);

    ui.wrap->setChecked(wrap);
    connect(ui.wrap, &QCheckBox::stateChanged, this, &FindDialog::onWrap);

    ui.exprBox->setPlaceholderText(tr("Find expr..."));
    connect(ui.exprBox, &QLineEdit::returnPressed, this, &FindDialog::onFind);

    connect(ui.cancel, &QPushButton::clicked, this, &FindDialog::onCancel);

    ui.find->setDefault(true);
    connect(ui.find, &QPushButton::clicked, this, &FindDialog::onFind);


    ui.layout->addWidget(ui.regex, 0, 0);
    ui.layout->addWidget(ui.caseSense, 0, 1);
    ui.layout->addWidget(ui.select, 0, 2);
    ui.layout->addWidget(ui.whole, 1, 0);
    ui.layout->addWidget(ui.forward, 1, 1);
    ui.layout->addWidget(ui.wrap, 1, 2);
    ui.layout->addWidget(ui.exprBox, 2, 0, 1, 3);
    ui.layout->addWidget(ui.cancel, 3, 1);
    ui.layout->addWidget(ui.find, 3, 2);

    setLayout(ui.layout);

    setWindowTitle(tr("Find"));
}
FindDialog::~FindDialog() {
    delete ui.layout;
    delete ui.regex;
    delete ui.caseSense;
    delete ui.select;
    delete ui.whole;
    delete ui.forward;
    delete ui.wrap;
    delete ui.exprBox;
    delete ui.cancel;
    delete ui.find;
}

void FindDialog::onCancel() {
    done(QDialog::Rejected);
}
void FindDialog::onFind() {
    expr = ui.exprBox->text();
    done(QDialog::Accepted);
}

void FindDialog::onRegex() { regex = ui.regex->isChecked(); }
void FindDialog::onCase() { caseSensitive = ui.caseSense->isChecked(); }
void FindDialog::onSelect() { inSelection = ui.select->isChecked(); }
void FindDialog::onWhole() { wholeWord = ui.whole->isChecked(); }
void FindDialog::onForward() { forward = ui.forward->isChecked(); }
void FindDialog::onWrap() { wrap = ui.wrap->isChecked(); }

主:

#include <NIDE/FindDialog.hpp>

#include <QApplication>

int main(int argc, char **argv) {
    QApplication *app = new QApplication(argc, argv);
    FindDialog fd(NULL);

    app->setApplicationName("NIDE");

    fd.exec();
}

1 个答案:

答案 0 :(得分:2)

在避免使用Q_OBJECT宏时,您依赖于Qt的实施细节。连接到没有Q_OBJECT的类中声明的方法是未定义的行为。它在特定版本的Qt中工作的事实是一个愉快的巧合。

唉,你的代码做了很多不必要的事情。

  1. 没有必要在堆上保留Ui元素或QApplication实例。

  2. 所有onXxxx方法都没有理由,因为在通过exec()显示对话框时,您无法读取对话框的状态。在接受(或可能被拒绝)后,您只关心对话框的状态。

  3. 您可以使用QDialog::acceptQDialog::reject个广告位。

  4. 要在多个平台上正确显示对话框,您应该使用QDialogButtonBox而不是离散按钮。

  5. 最后,使用QDialog::exec()重新进入事件循环,让您认为代码是真的不同步。它是很难找到错误的根源。您只需show()对话框,并在单击“查找”按钮时对其发出的accepted信号作出反应。

  6. 以下代码试图成为一个合理正确的解决方案。它适用于Qt 4和Qt 5。

    // Interface
    #include <QDialog>
    #include <QGridLayout>
    #include <QCheckBox>
    #include <QLineEdit>
    #include <QDialogButtonBox>
    
    #if QT_VERSION<QT_VERSION_CHECK(5,0,0)
    #define Q_DECL_OVERRIDE
    #endif
    
    class FindDialog: public QDialog {
       struct Ui {
          QGridLayout layout;
          QCheckBox regex, caseSense, select, whole, forward, wrap;
          QLineEdit exprBox;
          QDialogButtonBox buttonBox;
          Ui(QWidget * widget);
       } m_ui;
       void get();
    public:
       bool regex, caseSensitive, inSelection, wholeWord, forward, wrap;
       QString expr;
    
       FindDialog(QWidget *parent = 0);
       ~FindDialog();
       void set();
       void done(int r) Q_DECL_OVERRIDE;
    };
    
    // Implementation
    FindDialog::Ui::Ui(QWidget * widget) :
       layout(widget),
       regex(tr("Regex")),
       caseSense(tr("Match case")),
       select(tr("In Selection")),
       whole(tr("Whole Word")),
       forward(tr("Forward")),
       wrap(tr("Wrap around")),
       buttonBox(QDialogButtonBox::Cancel)
    {
       layout.addWidget(&regex, 0, 0);
       layout.addWidget(&caseSense, 0, 1);
       layout.addWidget(&select, 0, 2);
       layout.addWidget(&whole, 1, 0);
       layout.addWidget(&forward, 1, 1);
       layout.addWidget(&wrap, 1, 2);
       layout.addWidget(&exprBox, 2, 0, 1, 3);
       layout.addWidget(&buttonBox, 3, 0, 1, 3);
       exprBox.setPlaceholderText(tr("Find expr..."));
       buttonBox.addButton(tr("Find"), QDialogButtonBox::AcceptRole);
    }
    
    FindDialog::FindDialog(QWidget *parent): QDialog(parent), m_ui(this),
       regex(false), caseSensitive(true), inSelection(false), wholeWord(true),
       forward(true), wrap(true)
    {
       set();
       connect(&m_ui.buttonBox, SIGNAL(rejected()), SLOT(reject()));
       connect(&m_ui.buttonBox, SIGNAL(accepted()), SLOT(accept()));
       setWindowTitle("Find");
    }
    
    FindDialog::~FindDialog() {}
    
    void FindDialog::done(int result)
    {
       get();
       QDialog::done(result);
    }
    
    void FindDialog::get()
    {
       regex = m_ui.regex.isChecked();
       caseSensitive = m_ui.caseSense.isChecked();
       inSelection = m_ui.select.isChecked();
       wholeWord = m_ui.whole.isChecked();
       forward = m_ui.forward.isChecked();
       wrap = m_ui.wrap.isChecked();
       expr = m_ui.exprBox.text();
    }
    
    void FindDialog::set()
    {
       m_ui.regex.setChecked(regex);
       m_ui.caseSense.setChecked(caseSensitive);
       m_ui.select.setChecked(inSelection);
       m_ui.whole.setChecked(wholeWord);
       m_ui.forward.setChecked(forward);
       m_ui.wrap.setChecked(wrap);
    }
    
    // Main
    #include <QApplication>
    #include <QMessageBox>
    
    int main(int argc, char **argv) {
       QApplication app(argc, argv);
       app.setApplicationName("NIDE");
       FindDialog fd;
       fd.show();
    #if QT_VERSION>=QT_VERSION_CHECK(5,0,0)
       // This can't be done in Qt4 without using moc
       QObject::connect(&fd, &QDialog::accepted, [&fd]{
          QMessageBox::information(NULL, "Find",
             QString("The user wants to find \"%1\"").arg(fd.expr));
       });
    #endif
       return app.exec();
    }