我的QDialog在关闭后导致seg故障。我在使用它之前使用malloc我的结构,设置是PSETTINGS类型,并且是一个私有变量。
MainWindow类:(在settingsDiag->exec()
发生了Seg Fault
Settings *settingsDiag = new Settings(this);
settingsDiag->exec();
设置类:
在头文件中:
typedef struct ConnSettings {
ConnSettings():ipAddr(""), alias("Local"), port(8000), isClient(false){}
QString ipAddr;
QString alias;
int port;
bool isClient;
} SETTINGS, *PSETTINGS;
在CPP文件中:
Settings::Settings(QWidget *parent) :
QDialog(parent),
ui(new Ui::Settings)
{
ui->setupUi(this);
QButtonGroup serviceGroup(ui->serviceBox);
QValidator *validPort = new QRegExpValidator(QRegExp("^\\d*$"), this);
QValidator *validIp = new QRegExpValidator(QRegExp("^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$"), this);
serviceGroup.addButton(ui->clientButton);
serviceGroup.addButton(ui->serverButton);
connect(ui->okCancel, SIGNAL(accepted()), this, SLOT(storeSettings()));
connect(ui->clientButton, SIGNAL(toggled(bool)), this, SLOT(enableDisableClient(bool)));
ui->portText->setValidator(validPort);
ui->ipText->setValidator(validIp);
}
Settings::~Settings() {
delete ui;
}
void Settings::storeSettings() {
settings = (PSETTINGS)malloc(sizeof(SETTINGS));
settings->port = ui->portText->text().toInt();
if((settings->isClient = ui->clientButton->isChecked())) {
settings->ipAddr = ui->ipText->text();
settings->alias = ui->aliasText->text();
}
}
PSETTINGS Settings::getSettings() {
return settings;
}
void Settings::enableDisableClient(bool client) {
ui->clientBox->setEnabled(client);
}
谢谢:)
答案 0 :(得分:1)
malloc也引发了问题。该结构中的字符串无需构造即可使用。假设您的堆在该点之后损坏。
而不是malloc试试......
settings = new SETTINGS();
...当然,当你完成它时会删除。
答案 1 :(得分:0)
您正在堆栈上分配QButtonGroup serviceGroup。可能不是你想做的事。