Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. program crashes after assigning values to array elements
Qt 6.11 is out! See what's new in the release blog

program crashes after assigning values to array elements

Scheduled Pinned Locked Moved Solved General and Desktop
32 Posts 4 Posters 8.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R rezaMSLM

    @jsulm

    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?

    J.HilkJ Offline
    J.HilkJ Offline
    J.Hilk
    Moderators
    wrote on last edited by
    #6

    @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


    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


    Q: What's that?
    A: It's blue light.
    Q: What does it do?
    A: It turns blue.

    1 Reply Last reply
    2
    • R rezaMSLM

      @jsulm

      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?

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #7

      @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 declare

      private:
      int m_address[20][2];
      

      ?
      Back to the crash: what is it? Is it SIGSEGV? Or something else?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      R 1 Reply Last reply
      1
      • jsulmJ jsulm

        @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 declare

        private:
        int m_address[20][2];
        

        ?
        Back to the crash: what is it? Is it SIGSEGV? Or something else?

        R Offline
        R Offline
        rezaMSLM
        wrote on last edited by
        #8

        @jsulm

        @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?

        jsulmJ 1 Reply Last reply
        0
        • R rezaMSLM

          @jsulm

          @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?

          jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #9

          @rezaMSLM Start your app and when it crashes see what is printed in the console.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          R 1 Reply Last reply
          0
          • jsulmJ jsulm

            @rezaMSLM Start your app and when it crashes see what is printed in the console.

            R Offline
            R Offline
            rezaMSLM
            wrote on last edited by
            #10

            @jsulm

            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.
            
            jsulmJ 1 Reply Last reply
            0
            • R rezaMSLM

              @jsulm

              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.
              
              jsulmJ Online
              jsulmJ Online
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #11

              @rezaMSLM Did you see what @J-Hilk wrote?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              R 1 Reply Last reply
              0
              • jsulmJ jsulm

                @rezaMSLM Did you see what @J-Hilk wrote?

                R Offline
                R Offline
                rezaMSLM
                wrote on last edited by rezaMSLM
                #12

                @jsulm said in program crashes after assigning values to array elements:

                @rezaMSLM Did you see what @J-Hilk wrote?

                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

                jsulmJ 1 Reply Last reply
                0
                • R rezaMSLM

                  @jsulm said in program crashes after assigning values to array elements:

                  @rezaMSLM Did you see what @J-Hilk wrote?

                  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

                  jsulmJ Online
                  jsulmJ Online
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #13

                  @rezaMSLM To be sure: if on_pushButton_apply_clicked() is not executed there is no crash?
                  If so what happens if you comment out

                  m_address.WaterMeterIDs[i][j] = ui->tableWidget->item(i,j)->text().toInt();
                  

                  ?
                  And do you use m_address.WaterMeterIDs somewhere else?

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  R 1 Reply Last reply
                  0
                  • Maaz MominM Offline
                    Maaz MominM Offline
                    Maaz Momin
                    wrote on last edited by
                    #14

                    @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 :)

                    R 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @rezaMSLM To be sure: if on_pushButton_apply_clicked() is not executed there is no crash?
                      If so what happens if you comment out

                      m_address.WaterMeterIDs[i][j] = ui->tableWidget->item(i,j)->text().toInt();
                      

                      ?
                      And do you use m_address.WaterMeterIDs somewhere else?

                      R Offline
                      R Offline
                      rezaMSLM
                      wrote on last edited by
                      #15

                      @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

                      jsulmJ Maaz MominM 2 Replies Last reply
                      0
                      • R rezaMSLM

                        @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

                        jsulmJ Online
                        jsulmJ Online
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #16

                        @rezaMSLM Well, then the only idea I have is that you're writing outside of the array boundaries.

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • R rezaMSLM

                          @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

                          Maaz MominM Offline
                          Maaz MominM Offline
                          Maaz Momin
                          wrote on last edited by
                          #17

                          @rezaMSLM can you add another qDebug before m_address.WaterMeterIDs[i][j] = ui->tableWidget->item(i,j)->text().toInt();

                          qDebug() << i << j;

                          R 1 Reply Last reply
                          0
                          • Maaz MominM Maaz Momin

                            @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 :)

                            R Offline
                            R Offline
                            rezaMSLM
                            wrote on last edited by
                            #18

                            @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

                            1 Reply Last reply
                            0
                            • Maaz MominM Maaz Momin

                              @rezaMSLM can you add another qDebug before m_address.WaterMeterIDs[i][j] = ui->tableWidget->item(i,j)->text().toInt();

                              qDebug() << i << j;

                              R Offline
                              R Offline
                              rezaMSLM
                              wrote on last edited by
                              #19

                              @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

                              J.HilkJ 1 Reply Last reply
                              0
                              • R rezaMSLM

                                @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

                                J.HilkJ Offline
                                J.HilkJ Offline
                                J.Hilk
                                Moderators
                                wrote on last edited by
                                #20

                                @rezaMSLM can you make a minimal compileable example, so that we could test it?


                                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                Q: What's that?
                                A: It's blue light.
                                Q: What does it do?
                                A: It turns blue.

                                1 Reply Last reply
                                0
                                • Maaz MominM Offline
                                  Maaz MominM Offline
                                  Maaz Momin
                                  wrote on last edited by
                                  #21

                                  @rezaMSLM I now believe the issue is different here. As you say, you resize or click... That means a paint is called. I guess your table index or a pointer is getting invalid. Can only understand after looking into code, can you share it?

                                  R 1 Reply Last reply
                                  0
                                  • Maaz MominM Maaz Momin

                                    @rezaMSLM I now believe the issue is different here. As you say, you resize or click... That means a paint is called. I guess your table index or a pointer is getting invalid. Can only understand after looking into code, can you share it?

                                    R Offline
                                    R Offline
                                    rezaMSLM
                                    wrote on last edited by
                                    #22

                                    @Maaz-Momin said in program crashes after assigning values to array elements:

                                    @rezaMSLM I now believe the issue is different here. As you say, you resize or click... That means a paint is called. I guess your table index or a pointer is getting invalid. Can only understand after looking into code, can you share it?

                                    @J.Hilk said in program crashes after assigning values to array elements:

                                    can you make a minimal compileable example, so that we could test it?

                                    yes
                                    I will make a minimal compilable example and share it

                                    1 Reply Last reply
                                    0
                                    • R Offline
                                      R Offline
                                      rezaMSLM
                                      wrote on last edited by rezaMSLM
                                      #23

                                      https://ufile.io/g2azi
                                      I built a minimal compilable example that only read table cells and put them in array elements.
                                      it doesn't crashes!
                                      so I uploaded my current crashing program.
                                      after run choose setaddres from parameters menu and then set number of rows with spinbox and click set button
                                      then fill cells and then click apply.
                                      if for first time program didn't crashed close it and re run

                                      jsulmJ J.HilkJ 2 Replies Last reply
                                      0
                                      • R rezaMSLM

                                        https://ufile.io/g2azi
                                        I built a minimal compilable example that only read table cells and put them in array elements.
                                        it doesn't crashes!
                                        so I uploaded my current crashing program.
                                        after run choose setaddres from parameters menu and then set number of rows with spinbox and click set button
                                        then fill cells and then click apply.
                                        if for first time program didn't crashed close it and re run

                                        jsulmJ Online
                                        jsulmJ Online
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #24

                                        @rezaMSLM It did not compile on my Linux machine until I fixed all wrong connect() calls.
                                        It does not crash here, not in debug and not in release mode.

                                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        R 1 Reply Last reply
                                        1
                                        • jsulmJ jsulm

                                          @rezaMSLM It did not compile on my Linux machine until I fixed all wrong connect() calls.
                                          It does not crash here, not in debug and not in release mode.

                                          R Offline
                                          R Offline
                                          rezaMSLM
                                          wrote on last edited by
                                          #25

                                          @jsulm said in program crashes after assigning values to array elements:

                                          It does not crash here, not in debug and not in release mode.

                                          on my pc also doesn't crash some times
                                          set number of rows to something near 20 like 17
                                          and test it several times(each time close all windows and run it again)

                                          jsulmJ 1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved