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

Error: Application crashes after sending signal.



  • I am new to using Qt. I am trying to implement the game of Minesweeper in Qt using the calculator example from Qt Creator as a guide. I want the application to clear the cells around the one the user clicked if said cell has a 0 value, but the function I created for this purpose (clearAdjacent() in board.cpp) causes the application to crash if there are other cells with 0 around the one that was clicked. I could not pinpoint where the problem is but I think it is either in the leftClick() or clearCell() function in cell.cpp. Can someone help me, please?

    Additional information:
    Comment out lines 57 and 58 of cell.cpp to make the application work.
    bombFired() signal in cell.h is connected to gameOver() slot in board.cpp.
    zeroCell() signal in cell.h is connected to clearAdjacent() slot in board.cpp.

    Link to project: https://drive.google.com/drive/folders/1mTkPrRyn0PC013jqVcrIPE5rN3xizkfL?usp=sharing



  • Normally, containers are from 0 to (myContainer.count()-1).
    I see you are checking for

    (i2 >= 1 && i2 <= rows && j2 >= 1 && j2 <= columns)
    

    (no idea what "rows" and "columns" actually is, I'd use matrix.count() / matrix.first().count for that, if possible).

    I'd deem it possible that this leads to a call of

    matrix[i2][j2]
    

    where i2 and/or j2 go out of range. This would definitely lead to crashes.

    Make extensive use of

    qDebug()<<"myvariable says: "<<myvariable;
    

    to check where exactly the code fails.

    I hope this helps and that I have understood the problem.



  • Thank you for the response. I solved the error. When there were two consecutive cells with a value of 0 the cells around the first one would be clicked, but once the other cell was clicked the application would click the cells around that one and try to click the first cell and as a result go into an infinite loop. The solution was to give a variable to each cell that tracks whether they have been cleared so the clearAdjacent function would not clear a cell more than once. The files I shared are updated with the changes.


Log in to reply