Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

The program crashes suddenly when working with a new feature



  • Hi all,

    I'm talking about the spreadsheet program in the chapters 3 and 4 of the book C++ GUI Programming with Qt4 2nd Edition.
    There, I was about to add a new feature sum to the context menu this way:

    void MySpreadsheet::createContextMenu()
    {
       ...
        spreadsheet->addAction(sumAction);
       ...
        spreadsheet->setContextMenuPolicy(Qt::ActionsContextMenu);
    }
    
    void MySpreadsheet::createActions()
    {
       ...
      sumAction = new QAction(tr("&Sum"), this);
      sumAction->setStatusTip(tr("Sum the current selection's "
                                   "contents"));
     connect(sumAction, SIGNAL(triggered()), spreadsheet, SLOT(sum()));
     ...
    }
    
    void Spreadsheet::sum()
    {
        QList<QTableWidgetItem *> items = selectedItems();
        int i = 0, j = 0;
        double d = 0;
    
        if(!items.isEmpty()) {
            foreach (QTableWidgetItem* item, items)
                d += item->text().toDouble();
    
            position(items, i, j);
    
            QTableWidgetItem* item = new QTableWidgetItem;
            item->setText((QString::number(d)));
            ++i;
    
            item->setTextAlignment(Qt::AlignRight);
            QTableWidget::setItem(i, j, item);
            somethingChanged();
        }
    }
    
    void Spreadsheet::position(QList<QTableWidgetItem *> items, int& i, int& j)
    {
        if(!items.isEmpty()) {
            QTableWidgetItem* last = items.last();
            i = last->row();
            j = last->column();
        }
    }
    

    When working with this new feature normally, I mean, at times performing the sum action and then going for other tasks, then coming back to perform sum once again when needed, it works well. But when repeatedly performing the sum action one after another, especially when the range is still selected (highlighted) for the previous sum:

    0_1551778869812_2.png

    the program sometimes crashes with this error message:

    Starting D:\Projects\Qt\MySpreadsheet\MySpreadsheet\build-MySpreadsheet-Desktop_Qt_5_12_0_MinGW_64_bit-Debug\debug\MySpreadsheet.exe...
    The program has unexpectedly finished.
    The process was ended forcefully.
    D:/Projects/Qt/MySpreadsheet/MySpreadsheet/build-MySpreadsheet-Desktop_Qt_5_12_0_MinGW_64_bit-Debug/debug/MySpreadsheet.exe crashed.

    I guess, it's of the selection range when we select the new range for a new sum action while part of it is beforehand selected.

    Is this guess right? If so, how to solve this problem please?



  • @tomy
    I don't know, but:

    • ++i; QTableWidget::setItem(i, j, item);: How many rows does your QTableWidget start with? Do you need to call https://doc.qt.io/qt-5/qtablewidget.html#setRowCount to increase it before you try to setItem() in the new row at the end?

    • somethingChanged();: What does this do, does it have any relevant side-effects?

    • If you know how to use the debugger for MinGW, you will get more information about exactly where/why the crash is happening if you let it "crash" while inside the debugger?


  • Lifetime Qt Champion

    @tomy Please run through debugger and post the stack trace here...



  • @JonB

    I don't know, but:

    QTableWidget::setItem(i, j, item); sets the item (which is a double number, the sum of numbers) into the cell with row and column numbers i, j. The numbers i, j have been previously set by the position function, void position(QList<QTableWidgetItem *>, int&, int&). That function gives i the last row number:

    QTableWidgetItem* last = items.last();
           i = last->row();
    

    Then we increase it, ++i;, to refer to the next (blank) cell where we want to put the result of the sum into.

    Is there still any ambiguity about this section please?

    • somethingChanged();: What does this do, does it have any relevant side-effects?

    It's somethingChanged(). I mentioned the name of the book and chapters and thought every Qt programmer has (read) that book.
    If needed, please tell me and then I upload the project for further scrutiny.

    void Spreadsheet::somethingChanged()
    {
        if(autoRecalc)
            recalculate();
        emit modified();
    }
    
    • If you know how to use the debugger for MinGW, you will get more information about exactly where/why the crash is happening if you let it "crash" while inside the debugger?

    Months ago, when I for the first time was manipulating this project, I became a little familiar running the project by Debugger. But after a huge gap that I recently came back to Qt, I don't know much of that.
    I just looked at the Debub menu which is grayed out. I think I should put some breakpoints to be able to run the code by Debugger in order to be able to see the "crash" while inside the debugger. right?

    Is pressing F5 enough?



  • Just run using the debugger, crash your program and look at the stack trace window



  • @tomy said in The program crashes suddenly when working with a new feature:

    Then we increase it, ++i;, to refer to the next (blank) cell where we want to put the result of the sum into.

    Incrementing i means you are now referring to the next row. How do you/we know whether there is a next row in your QTableWidget? I gave you a reference to QTableWidget::setRowCount(), which I said you may need to call to increase how many rows there are, I don't know.

    Meanwhile, as I & the others have said, if you can use the debugger it may be the quickest way to get to whatever your problem is.



  • @JonB
    There are 1000 rows in each sheet while I'm working on the rows number 1 to 30 utmost. Yes, it would be better to use that function you sent, but it's not improving the code for those rows. I will make use of that afterwards. so thanks.



  • @VRonin

    It's more than 20 times I'm testing the program after running it by F5. But extremely astonishing, the program doesn't crash!!!! I changed nothing in the code.

    I should dedicate more time and hopefully see it crash. But, where is the window called "stack trace window", please?

    0_1551787511732_Capture.PNG



  • @tomy said in The program crashes suddenly when working with a new feature:

    where is the window called "stack trace window", please?

    It's the one in the bottom right corner



  • I ran the program with Debugger (F5) and tested it for crash many a time, but thus far it's never crashed in this mode. But when I run it by crtl+R, it crashes at the second or third time I apply the sum action on the highlighted cells!
    Why please? Does the debugger change the behavior of the program, please?

    Here is the project spreadsheet.rar. Please if possible test it and let me know what makes the program crash, please?
    I wanted to upload it here but faced a message saying I haven't that privilege! so posted that below:
    https://www.dropbox.com/s/anuk091qr8tjeem/spreadsheet.rar?dl=0


  • Lifetime Qt Champion

    Hi,

    Check for uninitialised pointers and/or variables. In debug mode, the pointers at least are initialised to zero but not in release mode (that's nothing Qt specific though)



  • I changed the initial value of some pointers from 0 to nullptr and also assigned 0 to some uninitialized variables too ( E.g., quint16) then reran the project and tested for the crash. This time I got this message at the time of crash:

    12:53:28: Starting C:\Users\Abbasi\Desktop\Documents\Qt\qt-book\chap03\build-spreadsheet-Desktop_Qt_5_12_0_MinGW_64_bit-Debug\debug\spreadsheet.exe...
    ASSERT: "size == 0 || offset < 0 || size_t(offset) >= sizeof(QArrayData)" in file..\..\include/QtCore/../../src/corelib/tools/qarraydata.h, line 67
    12:53:50: The program has unexpectedly finished.
    12:53:50: The process was ended forcefully.
    12:53:51: C:/Users/Abbasi/Desktop/Documents/Qt/qt-book/chap03/build-spreadsheet-Desktop_Qt_5_12_0_MinGW_64_bit-Debug/debug/spreadsheet.exe crashed.

    And when I clicked on that file I got this message:

    0_1552123884284_Capture.PNG

    By the way, I always run the programs in the Debug mode unless they're in their last state before creating an installer for them. So all crashes have happened in the Debug mode so far.

    And still by the Debugger, I get no crashes!!


  • Lifetime Qt Champion

    You're hitting an assertion because you are accesing a QVector/QList with an index which is out-of-bounds. Take a look at the backtrace to see from where the call is coming.

    So all crashes have happened in the Debug mode so far. And still by the Debugger, I get no crashes!!

    This somehow makes no sense from me - either it crashes in debug or not.


  • Moderators

    @Christian-Ehrlicher
    as far as i unerstand it, it‘s always a debug build but only crashes, when rhe debugger is not attached.

    which is unusual.

    You could potentially try attaching the debugger after wards to the running process via VS.


  • Lifetime Qt Champion

    Since it's an assertion it will also happen when a debugger is attached. And that's also the reason why it does 'work' in release mode.



  • @Christian-Ehrlicher

    Take a look at the backtrace to see from where the call is coming.

    As I said, when run by the Debugger, the project never crashes, at least based on my many tests' result!

    This somehow makes no sense from me - either it crashes in debug or not.

    I don't use the Release mode, so please let's drop this word down for the time being.

    I run the program only in the Debug mode, either by pressing F5 (which is the Debugger) or ctrl+R (which is a rormal run).

    • When run normally: Crash occurs.
    • when run by the Debugger: Crash doesn't occur.

    Why don't you simply download and run the project, please?


  • Lifetime Qt Champion

    @tomy said in The program crashes suddenly when working with a new feature:

    QTableWidgetItem* item = new QTableWidgetItem;

    This is wrong, you have to use 'Cell' ...

    Cell *Spreadsheet::cell(int row, int column) const
    {
        return static_cast<Cell *>(item(row, column));
    }
    


  • @Christian-Ehrlicher

    Do you mean I use sum() this way:

    void Spreadsheet::sum()
    {
        double d = 0.0;
    
        QList<QTableWidgetItem *> items = selectedItems();
    
        if(add(items, d)) {
            int row = 0, column = 0;
            position(items, row, column);
    
            ++row;
            Cell *c = cell(row, column);
            c->setTextAlignment(Qt::AlignRight);
            c->setText((QString::number(d)));
    
            somethingChanged();
        }
    }
    

    It still crashes.


  • Lifetime Qt Champion

    @tomy said in The program crashes suddenly when working with a new feature:

    It still crashes.

    But this time your debugger will show you where...



  • @Christian-Ehrlicher
    Right!

    0_1552139876140_4.PNG ![0_1552139887443_5.PNG]


  • Lifetime Qt Champion

    So when you go up the backtrace you see that "Cell *c = cell(row, column);" returns a nullptr.



  • @Christian-Ehrlicher

    So when you go up the backtrace you see that "Cell *c = cell(row, column);" returns a nullptr.

    Since it's empty, yeah?

    So how to get to the next row (which may or may not be empty) to set the result into it, please?


  • Lifetime Qt Champion

    When there is no object (yet) you should create one, or?



  • @Christian-Ehrlicher
    I was doing it by:

    QTableWidgetItem* item = new QTableWidgetItem;

    I don't know why we need: Cell *c = cell(row, column); while it's the last filled cell and we can't increase its row number to get to the next cell to put the result of sum() into it!


  • Lifetime Qt Champion

    I already told you that you don't have to use QTableWidgetItem but Cell since the model is working with Cell objects (which derives from QTableWidgetItem) instead QTableWidgetItem and therefore the static cast does undefined stuff -> c++ basics...



  • @Christian-Ehrlicher

    When there is no object (yet) you should create one, or?

    Using this statement: Cell* c = cell(row, column); we can only have c as a pointer to a non-empty cell in the sheet. Moreover, we can't make it point to the next cell as our target cell for putting the result of our function, because the program crashes. So that c is useless.

    What do you mean by creating an object? And object of what? A cell? If so, how? This way: Cell ce = *cell(row, column);?

    I really don't understand what your intention is.
    Isn't there any way to solve this issue?


  • Lifetime Qt Champion

    How to create a new object on the heap: http://www.cplusplus.com/reference/new/operator new/



  • @Christian-Ehrlicher

    You mean new? I've tested it beforehand in this version of sum and already for QTableWidgetItem.

    void Spreadsheet::sum()
    {
        double d = 0.0;
    
        QList<QTableWidgetItem *> items = selectedItems();
    
        if(add(items, d)) {
            int row = 0, column = 0;
            position(items, row, column);
    
            row++;
            
            Cell* c = new cell(row, column);
            c->setTextAlignment(Qt::AlignRight);
            c->setText((QString::number(d)));
           
            somethingChanged();
        }
    }
    

    Neither Cell* c = new cell(row, column); nor Cell* c = new Cell(row, column); works.

    Found it:

    void Spreadsheet::sum()
    {
        double d = 0.0;
    
        QList<QTableWidgetItem *> items = selectedItems();
    
        if(add(items, d)) {
            int row = 0, column = 0;
            position(items, row, column);
    
            row++;
    
            Cell* c = new Cell();
            c->setTextAlignment(Qt::AlignRight);
            c->setText((QString::number(d)));
            setItem(row, column, c);
            somethingChanged();
        }
    }
    

    And apparently works fine. :)


Log in to reply