关于QT在打开子窗口时程序崩溃的其中一个原因分析

版权声明:本博客内容为原创,若要转载,请注明出处!否则禁止转载! https://blog.csdn.net/wardenjohn/article/details/80163910

其实这个问题当时是纠结了我很长的一段时间,这段时间里面,我一直在网上面找相关的资料但是却没有有用的信息。

但是在后面的一个机缘巧合之下,我通过函数执行顺序来Debug,慢慢的发现问题出现在什么地方了。现在来总结一下这个问题吧。

其实我现在的经验觉得,对于QT里面(由于QT是基于C++的),不少的程序崩溃都可以往指针上面去靠拢。因为指针的指向错误或者指向了错误地区导致内存访问出错进而导致程序崩溃是经常出现的一种问题。现在我来说说我的问题。

我的主界面上面有一个按钮,按钮添加的槽就是在点击按钮以后,新创建一个对象,并且把这个对象给show出来。

但是这个时候经常就是会出现Program Unexpected Stop/Crash

我就很痛苦,通过程序执行顺序逻辑来查看里面的问题,一开始感觉没有什么问题,但是后面就开始发现不对劲了。

void MainWindow::enter_eigth_number()
{
    numWin = new enumber();
    numWin->show();
}

看这个代码,就是打开子窗口的槽函数,恩,现在还很好;

注释了numWin->show()发现崩溃现象仍然存在,定位在numWin产生对象时发生错误;

去看看numWin的函数

enumber::enumber(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::enumber)
{
    ui->setupUi(this);
    setWindowTitle("小小八数码");
    init_pic();
    connect(ui->close,SIGNAL(clicked(bool)),this,SLOT(close()));
    connect(ui->start,SIGNAL(clicked(bool)),this,SLOT(start()));
    connect(ui->load,SIGNAL(clicked(bool)),this,SLOT(loadp()));
}

怎么感觉还很好呢??

再看看init_pic();

void enumber::init_pic()
{
    QImage *ice;
    ice= new QImage(":/image/0");
    imgstack.push_back(*ice);
    ice= new QImage(":/image/1");
    imgstack.push_back(*ice);
    ice= new QImage(":/image/2");
    imgstack.push_back(*ice);
    ice= new QImage(":/image/3");
    imgstack.push_back(*ice);
    ice= new QImage(":/image/4");
    imgstack.push_back(*ice);
    ice= new QImage(":/image/5");
    imgstack.push_back(*ice);
    ice= new QImage(":/image/6");
    imgstack.push_back(*ice);
    ice= new QImage(":/image/7");
    imgstack.push_back(*ice);
    ice= new QImage(":/image/8");
    imgstack.push_back(*ice);

    loadp(open[0]);
}

恩,还没有什么问题啊?TAT


看看loadp()

void enumber::loadp(node *n)
{
    int width = ui->num00->width();
    int height = ui->num00->height();
    ui->num00->setPixmap(QPixmap::fromImage(imgstack[n->disk[0][0]]).scaled(width,height,Qt::IgnoreAspectRatio,Qt::SmoothTransformation));
    ui->num01->setPixmap(QPixmap::fromImage(imgstack[n->disk[0][1]]).scaled(width,height,Qt::IgnoreAspectRatio,Qt::SmoothTransformation));
    ui->num02->setPixmap(QPixmap::fromImage(imgstack[n->disk[0][2]]).scaled(width,height,Qt::IgnoreAspectRatio,Qt::SmoothTransformation));
    ui->num10->setPixmap(QPixmap::fromImage(imgstack[n->disk[1][0]]).scaled(width,height,Qt::IgnoreAspectRatio,Qt::SmoothTransformation));
    ui->num11->setPixmap(QPixmap::fromImage(imgstack[n->disk[1][1]]).scaled(width,height,Qt::IgnoreAspectRatio,Qt::SmoothTransformation));
    ui->num12->setPixmap(QPixmap::fromImage(imgstack[n->disk[1][2]]).scaled(width,height,Qt::IgnoreAspectRatio,Qt::SmoothTransformation));
    ui->num20->setPixmap(QPixmap::fromImage(imgstack[n->disk[2][0]]).scaled(width,height,Qt::IgnoreAspectRatio,Qt::SmoothTransformation));
    ui->num21->setPixmap(QPixmap::fromImage(imgstack[n->disk[2][1]]).scaled(width,height,Qt::IgnoreAspectRatio,Qt::SmoothTransformation));
    ui->num22->setPixmap(QPixmap::fromImage(imgstack[n->disk[2][2]]).scaled(width,height,Qt::IgnoreAspectRatio,Qt::SmoothTransformation));
}

哦豁!!!!!

你们发现了没有??

一开始的构造里面,调用了init_pic();

但是在init_pic()里面调用了loadp;

调用党的loadp()里面还调用了imstack这个没有初始化的东西,在当初写的时候在想到了后续初始化的情况,傻乎乎的忘记了在初始化时指针为空,可能出现越界访问的情况!!!

在构造中吧init_pic注释,OK!

猜你喜欢

转载自blog.csdn.net/wardenjohn/article/details/80163910