[英]Qt5 crash on close
我的项目有时会因崩溃而崩溃,但无法可靠地崩溃。 当使用“取消”按钮关闭时,它的崩溃频率要比使用退出键关闭的频率要少得多,这进一步使我感到困惑。 我有一个简单的查找对话框(一个较大的项目的一小部分),作为测试用例的一种形式。
尝试使用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();
}
为了避免使用Q_OBJECT
宏,您依赖于Qt的实现细节。 连接到没有Q_OBJECT
的类中声明的方法是不确定的行为。 它可以在特定版本的Qt中工作是一个偶然的巧合。
las,您的代码做了很多不必要的事情。
无需将Ui元素或QApplication
实例保留在堆上。
没有所有onXxxx
方法的理由,因为在通过exec()
显示对话框时,您不会读取对话框的状态。 您只关心对话框被接受(或拒绝)后的状态。
您可以利用QDialog::accept
和QDialog::reject
插槽。
若要使对话框在多个平台上正确显示,应使用QDialogButtonBox
而不是离散按钮。
最后,使用QDialog::exec()
重新进入事件循环,并让您认为您的代码实际上是同步的。 这是很难发现错误的来源。 您应该只show()
对话框,并对单击“查找”按钮时发出的已accepted
信号做出反应。
下面的代码试图作为一种合理正确的解决方案。 它可以在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(®ex, 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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.