Solved The inferior stopped because it triggered an exception
-
A follow up on my last unsolved post. I have traced the problem down to the fact that my program crashes when I try to update my slider, however, this only happens when I try to update the
ui's slider
. For example if I run the following code:TestController::TestController(QWidget *parent) : QMainWindow(parent), ui(new Ui::TestController), newSlider(*ui->horizontalSlider), newTextBox(*ui->spinBox), controller(newSlider,newTextBox,newTestClass) { ui->setupUi(this); QSlider *slider = new QSlider; QSlider& ples = *slider; ples.setValue(20); std::cout << ples.value() << std::endl; Slider *slider2 = new Slider; Slider& test = *slider2; test.setValue(30); std::cout << test.value() << std::endl; }
This will compile and give me 20 and 30 as expected when the program launches. However, if I even touch the slider on my gui, or my text box, the program immediately crashes and I am given the error:
The inferior stopped because it triggered an exception. Stopped in thread 0 by: Exception at ... : read access violation at ..., flags=0x0(first chance).
I am confused as to why this:
Slider& uiSlider = *ui->horizontalSlider; uiSlider.setValue(35); std::cout << uiSlider.value() << std::endl;
crashes the program entirely since
ui->horizontalSlider
has been promoted to the classSlider
. All my header file:class Slider : public QSlider { Q_OBJECT public: Slider(QWidget *parent = nullptr); void updateValue(int value) { this->setValue(value);} int get() const { return this->value(); } };
Does is update the value of the slider and get the value of the slider. And honestly, the stack trace doesn't tell me much either which is also super confusing to me:
Because at this point in the code I am not even calling
updateValue()
. I'm really stuck and I really don't know what to do from here. Any help would be greatly appreciated.[0_1557248297459_Minimal.zip](Uploading 100%)EDIT: Apparently
Slider &uiSlider = *ui->horizontalSlider;
does not break the code. It was a call in my controller that was causing it to break. -
A follow up on my last unsolved post.
So why are you creating a new post? This is bad practise. you could have easily provided the new information there.
That said, if you can upload your (minlmal) compile- and executable code somewhere, we could just try it.
Regards
Edit:
Slider& uiSlider = *ui->horizontalSlider;
What is that? Please elaborate.
-
I'm trying to upload the .zip file but I do not have enough privileges to do so. So I have uploaded it to my github account. You will need a C++17 Compiler in order to run the example by the way. This is the best I can do since I don't have the privileges to upload a .zip file. Or, I can copy paste all of the code including the ui file, please let me know which is the easiest.
Now to elaborate,
my Controller class takes classes by reference and not by pointers, so I turn the pointer into a reference so I can actually perform the task I am trying to accomplish.
Basically, it is one massive sync at a time, I create a
Controller<Slider,SpinBox,CustomClass> controller;
. I then can set all of their values by callingcontroller.setValues(35);
Which then calls Slider's, SpinBox's, and CustomClass'updateValue
function which will call their respective setters. In this case,
this->setValue(int)
for the Slider and the SpinBox, andsetVal(int)
for CustomClass. So the reason I do that, is so I can pass the reference to the horizontal slider into my controller class. However, as you will see in my header file I have:Slider &newSlider; Controller<Slider> controller;
I then initialize them in the .cpp file by:
newSlider(*ui->horizontalSlider); controller(newSlider);
But, this breaks whenever I try to call
controller.setValues(int);
. Or if I try to callnewSlider.setValue(int)
. However, if in the constructor I do something like:Slider &test = *ui->horizontalSlider; test.setValue(int); std::cout << ui->horizontalslider->value() << std::endl;
It actually doesn't break. Which confuses me a lot.
-
SOLVED
After conversing with someone, it was determined that the
ui->horizontalSlider
was not yet initialized so I was not pointing to anything. To solve this, I then created aSlider
pointer and aController
pointer. I then initialized them in the scope of the constructor and now everything works as desired.