Solved program crashes after assigning values to array elements
-
I have a class with name setaddress containing a structure that contains a 2D array:
int WaterMeterIDs[20][2];
namespace Ui { class SetAddress; } class SetAddress : public QDialog { Q_OBJECT public: struct AddressList{ int WaterMeterIDs[20][2];//the array }; explicit SetAddress(QWidget *parent = 0); ~SetAddress(); etc... private: Ui::SetAddress *ui; AddressList m_address;
I want to save my data placed in qtablewidget cells to the array using these commands.
.cpp file:void SetAddress::on_pushButton_apply_clicked() { int rowscount = ui->tableWidget->rowCount(); //rowscount is always less than 20 for(int j = 0; j < 2; j++){ for(int i = 0; i < rowscount; i++){ if(ui->tableWidget->item(i,j) != 0x0 ){//if cell is not empty m_address.WaterMeterIDs[i][j] = ui->tableWidget->item(i,j)->text().toInt();//convert data to int and put it in array qDebug()<<m_address.WaterMeterIDs[i][j]; } } } }
when I click on apply button the program works good(I can see array elements using qDebug()). but after I pressed the apply button if I press any other key even the close button(or even if I want to resize the window) program crashes!
-
@rezaMSLM Please post the stack trace after crash.
Also, you do not check whether rowscount is bigger than 20! -
@jsulm one note:
it doesn't crash in debug mode
only in release mode crashes! -
@rezaMSLM said in program crashes after assigning values to array elements:
it doesn't crash in debug mode
only in release mode crashes!Strong sign for something wrong in your code.
You probably overwrite some memory.
Did you check that rowscount is <= 20?Is there a reason why you have AddressList struct and why it is in public section?
-
if((ui->tableWidget->item(i,j) != 0x0) && (rowscount <= 20)){/*if cell is not empty && rowscount <= 20*/
I added rowscount check to the code
problem exists@jsulm said in program crashes after assigning values to array elements:
Is there a reason why you have AddressList struct and why it is in public section?
no there is not special reason. is it unusual?
-
@rezaMSLM said in program crashes after assigning values to array elements:
if((ui->tableWidget->item(i,j) != 0x0) && (rowscount <= 20)){/if cell is not empty && rowscount <= 20/
that is wrong, your array goes from 0 - 19, 20 is an invalid access
-
@rezaMSLM said in program crashes after assigning values to array elements:
no there is not special reason. is it unusual?
I just don't see the point to have a struct containing an array and nothing else.
And everything not needed outside the class should be inside private section (information hiding).
Why not just declareprivate: int m_address[20][2];
?
Back to the crash: what is it? Is it SIGSEGV? Or something else? -
@jsulm said in program crashes after assigning values to array elements:
I just don't see the point to have a struct containing an array and nothing else.
I'm planning to put and use some variables to the structure after solving this issue.
@jsulm said in program crashes after assigning values to array elements:
what is it? Is it SIGSEGV? Or something else?
how to know that?
-
@rezaMSLM Start your app and when it crashes see what is printed in the console.
-
The program has unexpectedly finished. The process was ended forcefully. D:/Projects/Qt_ProjectFolder/build-Meter_Config-Desktop_Qt_5_9_6_MinGW_32bit-Release/release/Meter_Config.exe crashed.
-
-
@jsulm said in program crashes after assigning values to array elements:
yes
if rowscount = 20 in for loop i have:for(int i = 0; i < count; i++){
meaning that maximum i = 19
although that is for safe side.
the number of rows in my tests is less than 10 -
@rezaMSLM To be sure: if on_pushButton_apply_clicked() is not executed there is no crash?
If so what happens if you comment outm_address.WaterMeterIDs[i][j] = ui->tableWidget->item(i,j)->text().toInt();
?
And do you use m_address.WaterMeterIDs somewhere else? -
@rezaMSLM Do you have any pointer or member variable that doesn't have a default value. It doesn't crash in "Debug" mode because Debug mode initializes a few things and release mode doesn't. Hope this helps you in figuring out what is wrong :)
-
@jsulm said in program crashes after assigning values to array elements:
what happens if you comment out
m_address.WaterMeterIDs[i][j] = ui->tableWidget->item(i,j)->text().toInt();if comment this, will not crash
@jsulm said in program crashes after assigning values to array elements:
And do you use m_address.WaterMeterIDs somewhere else?
no
-
@rezaMSLM Well, then the only idea I have is that you're writing outside of the array boundaries.
-
@rezaMSLM can you add another qDebug before m_address.WaterMeterIDs[i][j] = ui->tableWidget->item(i,j)->text().toInt();
qDebug() << i << j;
-
@Maaz-Momin said in program crashes after assigning values to array elements:
Do you have any pointer or member variable that doesn't have a default value
only WaterMeterIDs[20][2] has not initial value
-
@Maaz-Momin said in program crashes after assigning values to array elements:
@rezaMSLM can you add another qDebug before m_address.WaterMeterIDs[i][j] = ui->tableWidget->item(i,j)->text().toInt();
qDebug() << i << j;
added it.
the log is correct
and then crashes with one click anywhere -
@rezaMSLM can you make a minimal compileable example, so that we could test it?