[英]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: 我收到的错误来自Microsoft Visual C ++运行时库:
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:
这是我的c ++源文件:
#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. 如果复制
Sticks
对象,您也将复制指针ui
,因此您将在同一地址上多次删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.