【问题标题】:first-chance exception access violation writing location inside QT clicked eventQT点击事件内的第一次机会异常访问冲突写入位置
【发布时间】:2025-11-28 23:05:03
【问题描述】:

我有同一个应用程序的 2 个版本,在使用图形 UI 和 QT 的版本中,我遇到了运行时错误:

first-chance exception  access violation writing location 

参考

this->secondi_totali = someint;

在模型方法中启动。我的模型类是:

class Model {

public:

friend class Controller; //dico che controller può accedere alle cose private
//friend class cronometro_qt;


/*costruttore*/
Model();


void set(int,int,int);
int get_secondi();
int get_minuti();
int get_ore();
void tick();


private:
int secondi_totali;
int secondi_trascorsi;

int secondi;
int minuti;
int ore;

};

导致异常的方法是这样的:

void crono::Model::set(int ore, int minuti, int secondi) {
this->secondi_totali =  ore * 3600 + 60 * minuti + secondi;
this->secondi_trascorsi = 0;
 }

并在堆栈中从这个方法调用:

void crono::Controller::set(int ore, int minuti, int secondi) {

this->modello->set(ore, minuti, secondi);

}

this->modello 显然是一个指向 Controller 项内的 Model 对象的指针。

正如我之前所说,我已经完成了我的程序的 2 个版本。

首先,控制器的set()方法被一个空类视图的简单方法调用。

在第二个版本中,控制器的 set() 方法由 a 调用

void crono::cronometro_qt::on_pushButton_clicked() { /* ... */ }

方法。

显然,在这两个版本中,空视图或 cronometro_qt( QMainWindow 的子类)都有一个指向控制器的指针,以便像这样调用 set() 方法:

this->controller->set(ore,minuti, secondi);

那么为什么 QT 版本会抛出该异常?

【问题讨论】:

  • 您的模型指针似乎无效。消息的第二部分表明这只是一个分段错误,而不是异常(无论如何分配 int 时都不会发生)。
  • @FrankOsterfeld modello 指针在两个版本中的定义方式相同。无 QT 版本:nopaste.info/d0907b6843.html,这是 QT 版本:nopaste.info/48bb11482d.html
  • 这并没有显示指针在运行时是如何初始化的。在 set() 中传递 Model& 但存储指针看起来很可疑。
  • @FrankOsterfeld 我在这里粘贴了两个初始化:nopaste.info/6b58709c85.html,但它们是相同的。唯一的区别是,由于 QT-LESS 版本是演示版,它会自动启动 on_pushButton_clicked(),而不是像 QT 版本中那样等待用户点击 pushButton
  • 在初始化变量之前进入应用程序的事件循环(a.exec())。 exec() 在应用程序退出之前不会返回。将初始化移到 a.exec() 上方

标签: c++ qt exception


【解决方案1】:

您在初始化变量之前进入应用程序的事件循环 (a.exec())。 exec() 在应用程序退出之前不会返回。将初始化移到 a.exec() 上方。

【讨论】:

    最近更新 更多