简体   繁体   中英

QT project crashing on profile, running with release and debugger

I have been working on a project which features multiple games. I am having an error when the user selects one of the games, the application crashes. This only happens in profile mode.
The error I get is from microsoft visual C++ runtime library:

This applicaiton has request the Runtime to terminate in an unusual way...

From reading online it seems that I am doing improper memory managment, I can't seem to figure out where the issue is. Here is my c++ source file:

#include "sticks.h"
#include "ui_sticks.h"
Sticks::Sticks(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Sticks)
{
   ui->setupUi(this);
}

Sticks::~Sticks()
{
   delete ui;
}

int Sticks::selectUserChoice(int choice){
   ui->pushButton_select1->setEnabled(false);
   ui->pushButton_select2->setEnabled(false);
   ui->pushButton_select3->setEnabled(false);
   numsticks = numsticks - choice;
   //game_msg = "You grabbed " + QString::number(choice) + " sticks. There are " + QString::number(numsticks) + " sticks remaining";
   //setMessage(game_msg);
    computerChoice = getComputerChoice(numsticks);
    numsticks = numsticks - computerChoice;
   if (numsticks == 1)
       ui->pushButton_select1->setEnabled(true);
   if (numsticks == 2){
       ui->pushButton_select1->setEnabled(true);
       ui->pushButton_select2->setEnabled(true);
   }
   else{
       ui->pushButton_select1->setEnabled(true);
       ui->pushButton_select2->setEnabled(true);
       ui->pushButton_select3->setEnabled(true);
   }
}

   int Sticks::getComputerChoice(int current_sticks){
       /*Get a pseudo-random integer between 1 and 3 (inclusive)*/
       int choice = rand() % 3 + 1;

        if (current_sticks >=2 && current_sticks <=4){
            choice = current_sticks - 1;
    }
    else if (current_sticks == 1){
        return current_sticks;
    }
    return choice;
}

void Sticks::on_pushButton_select1_clicked()
{
selectUserChoice(1);
}

void Sticks::on_pushButton_select2_clicked()
{
   selectUserChoice(2);
}

void Sticks::on_pushButton_select3_clicked()
{
selectUserChoice(3);
}

void Sticks::on_pushButton_select_num_sticks_clicked()
{
    numsticks = ui->lineEdit_numsticks->text().toInt();
    if (numsticks < 10 || numsticks > 100) //Check that numsticks is 10-100
        return;
    std::ostringstream ss;
    ss << "There are " << numsticks << " sticks remaining";
    std::string str = ss.str();
    QString qstr = QString::fromStdString(str);
    ui->label_current_sticks->setText(qstr);
    //ui->horizontalLayout->removeWidget(ui->pushButton_select_num_sticks);
    delete ui->pushButton_select_num_sticks;
    delete ui->lineEdit_numsticks;
    ui->pushButton_select1->setEnabled(true);
    ui->pushButton_select2->setEnabled(true);
    ui->pushButton_select3->setEnabled(true);
    ui->label_grab->setEnabled(true);
}

void Sticks::setMessage(QString msg){
    /*
    prev_msg3 = ui->label_msg3->text();
    prev_msg2 = ui->label_msg2->text();
    prev_msg1 = ui->label_msg1->text();
    ui->label_msg4->setText(prev_msg3);
    ui->label_msg3->setText(prev_msg2);
    ui->label_msg2->setText(prev_msg1);
    ui->label_msg1->setText(msg);
    */
}

Your problem probably is in this two lines:

delete ui->pushButton_select_num_sticks;
delete ui->lineEdit_numsticks;

You are deleting something that (I suppose) you are not dynamically allocating.

And be careful with this destructor:

Sticks::~Sticks()
{
   delete ui;
}

If you copy the Sticks objects you will copy the pointer ui too, so you are going to delete on same address multiple times.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM