pointer changing, without using anything in code
-
__name__ CThreadController::CThreadController () main threcon = 0x557b040d76d0 CGUI::CGUI() CGUI::InitTestBoard InitTestBoard try begin InitTestBoard try 2 InitTestBoard try end CGUI::InitDistance CGUI::InitCalibration CGUI::InitMotor main gui = 0x557b040bdac0 **main cont = 0x557b045ce800** CThreadController gui = 0x557b040bdac0 **CThreadController cont = 0x557b045ce800** CGUI::SetController controller = 0x557b040d76d0 CControls::SetController begin CThreadController::startThreads() begin CThreadController::startThreads() end Btn_motor_update_Clicked begin CGUI::Btn_motor_update_Clicked pin = 12 CControls::SetNumPinPerBoard this = 0x557b045ce800 CControls::SetNumPinPerBoard n = 12 CGUI::Btn_motor_update_Clicked step = 4 CControls::SetStepMtrPst n = 4 **CControls::SetStepMtrPst this = 0x557b045ce800** CControls::SetStepMtrPst m_iStepMtrPst = 4 CGUI::Btn_motor_update_Clicked m_threadcontroller = 0x557b040d76d0 **CThreadController::SetNumMotorPos m_controls = 0x557b00000031** CControls::SetNumMotorPos begin **CControls::SetNumMotorPos this = 0x557b00000031** CControls::SetNumMotorPos m_iStepMtrPst = Press <RETURN> to close this window...m_threadcontroller is correct in class CGUI, but m_controls is randomly changed CThreadController.
sometimes m_controls changes to something that crashes program.
m_controls is set in only one place, and no second call to CThreadController constructor is made.
at first i thoughtconnect(m_gui, &CNC::CGUI::emitNumPinPerBoard, m_controls, &CNC::CControls::SetNumPinPerBoard);and
connect(m_gui, &CNC::CGUI::emitStepMtrPst, m_controls, &CNC::CControls::SetStepMtrPst);was not passing the values
not finished making all the thread connections. want to know why and how the pointer is changing
-
my code is not changing the value of the pointer. the pointer is randomly changing from outside of my code
-
int main(int argc, char **argv) { std::cout << "__name__\n" << std::flush; QApplication app (argc, argv); CNC::CThreadController *threcon = new CNC::CThreadController(); std::cout << "main threcon = " << threcon << '\n' << std::flush; CNC::CGUI *gui = new CNC::CGUI(); std::cout << "main gui = " << gui << '\n' << std::flush; CNC::CControls *cont = new CNC::CControls(); std::cout << "main cont = " << cont << '\n' << std::flush; threcon->SetGUI(gui); threcon->SetControls(cont); gui->SetController(threcon); cont->SetController(threcon); threcon->startThreads(); gui->show(); return app.exec(); //return 1; }void CThreadController::SetControls(CControls *cont) { std::cout << "CThreadController cont = " << cont << '\n' << std::flush; m_controls = cont; } void CThreadController::SetGUI(CGUI *gui) { std::cout << "CThreadController gui = " << gui << '\n' << std::flush; m_gui = gui; }void CGUI::SetController(CThreadController *controller) { std::cout << "CGUI::SetController controller = " << controller << '\n' << std::flush; m_threadcontroller = controller; }void CControls::SetController(CThreadController *controller) { std::cout << "CControls::SetController begin \n" << std::flush; m_threadcontroller = controller; }void CThreadController::startThreads() { std::cout << "CThreadController::startThreads() begin\n" << std::flush; //?? does (this) have to be sent to CNC::CControls(this);? //CNC::CControls *controlsThread = new CNC::CControls(this); //?? connect(m_controls, signal, this, slots); connect(m_controls, &CNC::CControls::finished, this, &QObject::deleteLater); connect(m_controls, &CNC::CControls::emitMessageBoxRed, m_gui, &CNC::CGUI::RecieveMessageBoxRed); //?? gui connect(m_gui, &CNC::CGUI::emitBitDia, m_controls, &CNC::CControls::SetBitDia); connect(m_gui, &CNC::CGUI::emitMove_To, m_controls, &CNC::CControls::Move_To); connect(m_gui, &CNC::CGUI::emitMove_A_axis, m_controls, &CNC::CControls::Move_A_axis); connect(m_gui, &CNC::CGUI::emitMove_B_axis, m_controls, &CNC::CControls::Move_B_axis); connect(m_gui, &CNC::CGUI::emitMove_C_axis, m_controls, &CNC::CControls::Move_C_axis); connect(m_gui, &CNC::CGUI::emitMove_X_axis, m_controls, &CNC::CControls::Move_X_axis); connect(m_gui, &CNC::CGUI::emitMove_Y_axis, m_controls, &CNC::CControls::Move_Y_axis); connect(m_gui, &CNC::CGUI::emitMove_Z_axis, m_controls, &CNC::CControls::Move_Z_axis); connect(m_gui, &CNC::CGUI::emitMin_A, m_controls, &CNC::CControls::SetMin_A); connect(m_gui, &CNC::CGUI::emitMin_B, m_controls, &CNC::CControls::SetMin_B); connect(m_gui, &CNC::CGUI::emitMin_C, m_controls, &CNC::CControls::SetMin_C); connect(m_gui, &CNC::CGUI::emitMin_X, m_controls, &CNC::CControls::SetMin_X); connect(m_gui, &CNC::CGUI::emitMin_Y, m_controls, &CNC::CControls::SetMin_Y); connect(m_gui, &CNC::CGUI::emitMin_Z, m_controls, &CNC::CControls::SetMin_Z); connect(m_gui, &CNC::CGUI::emitMax_A, m_controls, &CNC::CControls::SetMax_A); connect(m_gui, &CNC::CGUI::emitMax_B, m_controls, &CNC::CControls::SetMax_B); connect(m_gui, &CNC::CGUI::emitMax_C, m_controls, &CNC::CControls::SetMax_C); connect(m_gui, &CNC::CGUI::emitMax_X, m_controls, &CNC::CControls::SetMax_X); connect(m_gui, &CNC::CGUI::emitMax_Y, m_controls, &CNC::CControls::SetMax_Y); connect(m_gui, &CNC::CGUI::emitMax_Z, m_controls, &CNC::CControls::SetMax_Z); connect(m_gui, &CNC::CGUI::emitNumPinPerBoard, m_controls, &CNC::CControls::SetNumPinPerBoard); connect(m_gui, &CNC::CGUI::emitRun, m_controls, &CNC::CControls::Run); connect(m_gui, &CNC::CGUI::emitPortName_1, m_controls, &CNC::CControls::SetPortName_1); connect(m_gui, &CNC::CGUI::emitPortName_1, m_controls, &CNC::CControls::SetPortName_2); connect(m_gui, &CNC::CGUI::emitPortName_1, m_controls, &CNC::CControls::SetPortName_3); connect(m_gui, &CNC::CGUI::emitPortName_1, m_controls, &CNC::CControls::SetPortName_4); connect(m_gui, &CNC::CGUI::emitSer_1_connected, m_controls, &CNC::CControls::SetSer_1_connected); connect(m_gui, &CNC::CGUI::emitSer_2_connected, m_controls, &CNC::CControls::SetSer_2_connected); connect(m_gui, &CNC::CGUI::emitSer_3_connected, m_controls, &CNC::CControls::SetSer_3_connected); connect(m_gui, &CNC::CGUI::emitSer_4_connected, m_controls, &CNC::CControls::SetSer_4_connected); connect(m_gui, &CNC::CGUI::emitState, m_controls, &CNC::CControls::SetState); connect(m_gui, &CNC::CGUI::emitStepMtrPst, m_controls, &CNC::CControls::SetStepMtrPst); m_controls->start(); std::cout << "CThreadController::startThreads() end\n" << std::flush; }void CGUI::Btn_motor_update_Clicked() { std::cout << "Btn_motor_update_Clicked begin \n" << std::flush; try { //?? number pin per board ?? var name? int temp = (ui->textEdit_21->toPlainText()).toInt(); std::cout << "CGUI::Btn_motor_update_Clicked pin = " << temp << '\n' << std::flush; emit emitNumPinPerBoard(temp); temp = (ui->textEdit_20->toPlainText()).toInt(); std::cout << "CGUI::Btn_motor_update_Clicked step = " << temp << '\n' << std::flush; emit emitStepMtrPst(temp); std::cout << "CGUI::Btn_motor_update_Clicked m_threadcontroller = " << m_threadcontroller << '\n' << std::flush; m_threadcontroller->SetNumMotorPos(); std::cout << "Btn_motor_update_Clicked 4 \n" << std::flush; MessageBoxYellow("Number position on motors, & Number of pins per board, updated"); std::cout << "Btn_motor_update_Clicked 5 \n" << std::flush; } catch(std::exception& e) { MessageBoxError(e.what()); } std::cout << "Btn_motor_update_Clicked end \n" << std::flush; }void CControls::SetNumPinPerBoard(int n) { //std::cout << "CControls::SetNumPinPerBoard begin \n" << std::flush; std::cout << "CControls::SetNumPinPerBoard this = " << this << '\n' << std::flush; std::cout << "CControls::SetNumPinPerBoard n = " << n << '\n' << std::flush; m_iNumPinPerBoard = n; }void CControls::SetStepMtrPst(int n) { //std::cout << "CControls::SetStepMtrPst \n" << std::flush; std::cout << "CControls::SetStepMtrPst n = " << n << '\n' << std::flush; std::cout << "CControls::SetStepMtrPst this = " << this << '\n' << std::flush; m_iStepMtrPst = n; std::cout << "CControls::SetStepMtrPst m_iStepMtrPst = " << m_iStepMtrPst << '\n' << std::flush; }void CThreadController::SetNumMotorPos() { std::cout << "CThreadController::SetNumMotorPos m_controls = " << m_controls << '\n' << std::flush; m_controls->SetNumMotorPos(); }void CControls::SetNumMotorPos() { std::cout << "CControls::SetNumMotorPos begin \n" << std::flush; std::cout << "CControls::SetNumMotorPos this = " << this << '\n' << std::flush; std::cout << "CControls::SetNumMotorPos m_iStepMtrPst = " << std::flush; std::cout << m_iStepMtrPst << '\n' << std::flush; m_motor_A_axis.SetNumMotorPos(m_iStepMtrPst); m_motor_B_axis.SetNumMotorPos(m_iStepMtrPst); m_motor_C_axis.SetNumMotorPos(m_iStepMtrPst); m_motor_X_axis.SetNumMotorPos(m_iStepMtrPst); m_motor_Y_axis.SetNumMotorPos(m_iStepMtrPst); m_motor_Z_axis.SetNumMotorPos(m_iStepMtrPst); std::cout << "CControls::SetNumMotorPos end \n" << std::flush; }since the address to CControls object is set in main, and nothing in my code creates a new CThreadController object, why is CControls object changing?
-
__name__ CThreadController::CThreadController () main threcon = 0x564ac2f50fe0 CGUI::CGUI() CGUI::InitTestBoard InitTestBoard try begin InitTestBoard try 2 InitTestBoard try end CGUI::InitDistance CGUI::InitCalibration CGUI::InitMotor main gui = 0x564ac2f677c0 main cont = 0x564ac3478010 CThreadController gui = 0x564ac2f677c0 CThreadController cont = 0x564ac3478010 CGUI::SetController controller = 0x564ac2f50fe0 CControls::SetController begin CThreadController::startThreads begin CThreadController::startThreads m_controls = 0x564ac3478010 CThreadController::startThreads m_gui = 0x564ac2f677c0 CThreadController::startThreads this = 0x564ac2f50fe0 CThreadController::startThreads m_controls = 0x564ac3478010 CThreadController::startThreads m_gui = 0x564ac2f677c0 CThreadController::startThreads() end CThreadController::ShowPointers m_controls = 0x564ac3478010 CThreadController::ShowPointers m_gui = 0x564ac2f677c0 Btn_motor_update_Clicked begin CGUI::Btn_motor_update_Clicked pin = 12 CControls::SetNumPinPerBoard this = 0x564ac3478010 CControls::SetNumPinPerBoard n = 12 CGUI::Btn_motor_update_Clicked step = 4 CControls::SetStepMtrPst n = 4 CControls::SetStepMtrPst this = 0x564ac3478010 CControls::SetStepMtrPst m_iStepMtrPst = 4 CGUI::Btn_motor_update_Clicked m_threadcontroller = 0x564ac2f50fe0 CThreadController::SetNumMotorPos m_controls = 0x564ac3492190 CThreadController::SetNumMotorPos m_gui = 0x564a800000ee CControls::SetNumMotorPos begin CControls::SetNumMotorPos this = 0x564ac3492190 CControls::SetNumMotorPos m_threadcontroller = 0x7f74f0b38980 CControls::SetNumMotorPos m_iStepMtrPst = 0 CMotor::SetNumMotorPos begin num = 0 error: CMotor.SetNumMotorPos() Btn_motor_update_Clicked end Press <RETURN> to close this window...where and why is class data m_controls and m_gui changing in class CThreadController?
class data m_threadcontroller is not changing in class CGUI, but is changing in class CControls?
-
__name__ CThreadController::CThreadController () main threcon = 0x564ac2f50fe0 CGUI::CGUI() CGUI::InitTestBoard InitTestBoard try begin InitTestBoard try 2 InitTestBoard try end CGUI::InitDistance CGUI::InitCalibration CGUI::InitMotor main gui = 0x564ac2f677c0 main cont = 0x564ac3478010 CThreadController gui = 0x564ac2f677c0 CThreadController cont = 0x564ac3478010 CGUI::SetController controller = 0x564ac2f50fe0 CControls::SetController begin CThreadController::startThreads begin CThreadController::startThreads m_controls = 0x564ac3478010 CThreadController::startThreads m_gui = 0x564ac2f677c0 CThreadController::startThreads this = 0x564ac2f50fe0 CThreadController::startThreads m_controls = 0x564ac3478010 CThreadController::startThreads m_gui = 0x564ac2f677c0 CThreadController::startThreads() end CThreadController::ShowPointers m_controls = 0x564ac3478010 CThreadController::ShowPointers m_gui = 0x564ac2f677c0 Btn_motor_update_Clicked begin CGUI::Btn_motor_update_Clicked pin = 12 CControls::SetNumPinPerBoard this = 0x564ac3478010 CControls::SetNumPinPerBoard n = 12 CGUI::Btn_motor_update_Clicked step = 4 CControls::SetStepMtrPst n = 4 CControls::SetStepMtrPst this = 0x564ac3478010 CControls::SetStepMtrPst m_iStepMtrPst = 4 CGUI::Btn_motor_update_Clicked m_threadcontroller = 0x564ac2f50fe0 CThreadController::SetNumMotorPos m_controls = 0x564ac3492190 CThreadController::SetNumMotorPos m_gui = 0x564a800000ee CControls::SetNumMotorPos begin CControls::SetNumMotorPos this = 0x564ac3492190 CControls::SetNumMotorPos m_threadcontroller = 0x7f74f0b38980 CControls::SetNumMotorPos m_iStepMtrPst = 0 CMotor::SetNumMotorPos begin num = 0 error: CMotor.SetNumMotorPos() Btn_motor_update_Clicked end Press <RETURN> to close this window...where and why is class data m_controls and m_gui changing in class CThreadController?
class data m_threadcontroller is not changing in class CGUI, but is changing in class CControls?
@micha_eleric said in pointer changing, without using anything in code:
where and why is class data m_controls and m_gui changing in class CThreadController?
Provide a minimal, compilable example instead posting the whole question over and over without any real information.
-
@micha_eleric said in pointer changing, without using anything in code:
where and why is class data m_controls and m_gui changing in class CThreadController?
Provide a minimal, compilable example instead posting the whole question over and over without any real information.
@Christian-Ehrlicher said in pointer changing, without using anything in code:
@micha_eleric said in pointer changing, without using anything in code:
where and why is class data m_controls and m_gui changing in class CThreadController?
Provide a minimal, compilable example instead posting the whole question over and over without any real information.
i assume the minimum would be no code, since i can not find anything in my code to create the error. i posted everything of my code, where the error showed up. i keep adding more std::cout, trying to figure out between my code and my code it is happening.
-
added
std::cout << "CControls::SetNumMotorPos m_threadcontroller = " << m_threadcontroller << '\n' << std::flush;causes it to crash with multiple tries.
what i thought was a different m_threadcontroller in CControls, but was also a different CControls object, which i still question where the second CControls and CGUI object came from, when no new objects are created -
finally got a try to not crash
main threcon = 0x55b88df8dd00 CGUI::CGUI() CGUI::InitTestBoard InitTestBoard try begin InitTestBoard try 2 InitTestBoard try end CGUI::InitDistance CGUI::InitCalibration CGUI::InitMotor main gui = 0x55b88df72d40 main cont = 0x55b88e483610 CThreadController gui = 0x55b88df72d40 CThreadController cont = 0x55b88e483610 CGUI::SetController controller = 0x55b88df8dd00 CControls::SetController begin CThreadController::startThreads begin CThreadController::startThreads m_controls = 0x55b88e483610 CThreadController::startThreads m_gui = 0x55b88df72d40 CThreadController::startThreads this = 0x55b88df8dd00 CThreadController::startThreads m_controls = 0x55b88e483610 CThreadController::startThreads m_gui = 0x55b88df72d40 CThreadController::startThreads() end CThreadController::ShowPointers m_controls = 0x55b88e483610 CThreadController::ShowPointers m_gui = 0x55b88df72d40 Btn_motor_update_Clicked begin CGUI::Btn_motor_update_Clicked pin = 12 CControls::SetNumPinPerBoard this = 0x55b88e483610 CControls::SetNumPinPerBoard m_threadcontroller = 0x55b88df8dd00 CControls::SetNumPinPerBoard n = 12 CGUI::Btn_motor_update_Clicked step = 4 CControls::SetStepMtrPst this = 0x55b88e483610 CControls::SetStepMtrPst m_threadcontroller = 0x55b88df8dd00 CControls::SetStepMtrPst n = 4 CControls::SetStepMtrPst m_iStepMtrPst = 4 CGUI::Btn_motor_update_Clicked m_threadcontroller = 0x55b88df8dd00 CThreadController::SetNumMotorPos this = 0x55b88df8dd00 CThreadController::SetNumMotorPos m_controls = 0x55b88e6bd590 CThreadController::SetNumMotorPos m_gui = 0x55b8800000ef CControls::SetNumMotorPos begin CControls::SetNumMotorPos this = 0x55b88e6bd590 CControls::SetNumMotorPos m_threadcontroller = 0x7fe7fc8aa668 CControls::SetNumMotorPos m_iStepMtrPst = -11711155 CMotor::SetNumMotorPos begin num = -11711155 error: CMotor.SetNumMotorPos() Btn_motor_update_Clicked end Press <RETURN> to close this window...correct CThreadController points to wrong CControls points back to wrong CThreadControlls
-
change of pointers occur between
void CThreadController::startThreads()and
void CGUI::Btn_motor_update_Clicked()from what i can find
-
__name__ CThreadController::CThreadController () main threcon = 0x561d71086500 CGUI::CGUI CGUI::InitTestBoard InitTestBoard try begin InitTestBoard try 2 InitTestBoard try end CGUI::InitDistance CGUI::InitCalibration CGUI::InitMotor main gui = 0x561d710809a0 CControls::Constructor main cont = 0x561d71590fd0 CThreadController gui = 0x561d710809a0 CThreadController cont = 0x561d71590fd0 CGUI::SetController controller = 0x561d71086500 CControls::SetController begin CThreadController::startThreads begin CThreadController::startThreads m_controls = 0x561d71590fd0 CThreadController::startThreads m_gui = 0x561d710809a0 CThreadController::startThreads this = 0x561d71086500 CThreadController::startThreads m_controls = 0x561d71590fd0 CThreadController::startThreads m_gui = 0x561d710809a0 CThreadController::startThreads() end CThreadController::ShowPointers m_controls = 0x561d71590fd0 CThreadController::ShowPointers m_gui = 0x561d710809a0 Btn_motor_update_Clicked begin CThreadController::ShowPointers m_controls = 0x561d00000021 CThreadController::ShowPointers m_gui = 0x561d7172ae70 CGUI::Btn_motor_update_Clicked pin = 12 CControls::SetNumPinPerBoard this = 0x561d71590fd0 CControls::SetNumPinPerBoard m_threadcontroller = 0x561d71086500 CControls::SetNumPinPerBoard n = 12 CThreadController::ShowPointers m_controls = 0x561d00000021 CThreadController::ShowPointers m_gui = 0x561d7172ae70 CGUI::Btn_motor_update_Clicked step = 4 CControls::SetStepMtrPst this = 0x561d71590fd0 CControls::SetStepMtrPst m_threadcontroller = 0x561d71086500 CControls::SetStepMtrPst n = 4 CControls::SetStepMtrPst m_iStepMtrPst = 4 CGUI::Btn_motor_update_Clicked m_threadcontroller = 0x561d71086500 CThreadController::SetNumMotorPos this = 0x561d71086500 CThreadController::SetNumMotorPos m_controls = 0x561d00000021 CThreadController::SetNumMotorPos m_gui = 0x561d7172ae70 CControls::SetNumMotorPos begin CControls::SetNumMotorPos this = 0x561d00000021 Press <RETURN> to close this window...from std::cout output, it is clear the my code in main calls
threcon->startThreads();at the end of
void CThreadController::startThreads()the only 3 pointers my code creates/calls constructors, are what they are suppose to be.
nextthrecon->ShowPointers(); gui->show(); return app.exec();then button is pushed, calling
void CGUI::Btn_BoardUpdate_Clicked()which, without calling any constructors, there are 3 new pointers created out of thin air.
what/where/why are the 3 new pointers coming from?
only possible answers i come up with:
does qt create, create pointers that do not call constructors?
whenconnect(m_gui, &CNC::CGUI::emitRun, m_controls, &CNC::CControls::Run);does it create a duplicate pointers, which does not call constructors?
dont know what else could be happening.
if i reset the pointers in CThreadController, would whatever created new pointers, without calling a constructor, do it again. would i have to reset the pointers, every time a button is clicked? -
i tried a copy constructor, but it was not called
-
@Christian-Ehrlicher said in pointer changing, without using anything in code:
@micha_eleric said in pointer changing, without using anything in code:
where and why is class data m_controls and m_gui changing in class CThreadController?
Provide a minimal, compilable example instead posting the whole question over and over without any real information.
i assume the minimum would be no code, since i can not find anything in my code to create the error. i posted everything of my code, where the error showed up. i keep adding more std::cout, trying to figure out between my code and my code it is happening.
@micha_eleric said in pointer changing, without using anything in code:
i assume the minimum would be no code, since i can not find anything in my code to create the error.
Your assumption is very likely wrong. The thing is, with errors like this, it is very commonly the case that code elsewhere in your application has a memory error, but that the effect of that error is not visible until later (eg an earlier memory error could have overwritten your code segement, so your code no longer matches what you're reading in your editor). In other words, the bug might be nowhere near the crash (or pointer change) as far as lines of code goes.
There's a couple of approaches I've found to work in cases like this:
- use a static analysis tool, like cppcheck (it's not going to pick up subtle things, but its so quick and easy to start with).
- use a memory fencing tool, like valgrind
- slowly remove as much code as possible, including (especially) code elsewhere that you assume couldn't possibly be related to this area of the code (I've been surprised plenty of times ;)
In particular, the exercise of cutting it down to "a minimal, compilable example", as @Christian-Ehrlicher suggested, will effectively force you to do #3 anyway, and often you'll discover the bug in that process, before you even get to posting the minimal example. But if you do get it down to a minimal compilable example, do share it! :D
Good luck!
-
well, if i comment out
m_controls->start();in
// function void CThreadController::startThreads() { m_controls->start(); }then the pointers do not change
start();is not my code.