开发者

QDialog exec() (SEG FAULT)

开发者 https://www.devze.com 2023-02-14 14:11 出处:网络
My QDialog is causing a seg fault after it closes. I malloc my struct before using it, settings is of type PSETTINGS and is a private variable.

My QDialog is causing a seg fault after it closes. I malloc my struct before using it, settings is of type PSETTINGS and is a private variable.

MainWindow Class:(Seg Fault happens in settingsDiag->exec())

Settings *settingsDiag = new Settings(this);

settingsDiag->exec();

Settings Class:

In header file:

typedef struct ConnSettings {
    ConnS开发者_C百科ettings():ipAddr(""), alias("Local"), port(8000), isClient(false){}
    QString ipAddr;
    QString alias;
    int port;
    bool isClient;
} SETTINGS, *PSETTINGS;

In CPP file:

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);
}

Thanks :)


The malloc is causing problems too. The strings in that struct get used without being constructed. Assume your heap corrupted after that point.

Instead of malloc try...

settings = new SETTINGS();

... and of course delete when you are done with it.


You are allocating the QButtonGroup serviceGroup on the stack. Probably not what you want to do.

0

精彩评论

暂无评论...
验证码 换一张
取 消