Results 1 to 6 of 6

Thread: Crashes on run, works on debugging

  1. #1
    Join Date
    Sep 2019
    Posts
    20
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Question Crashes on run, works on debugging

    Hello!
    So the game works just fine but when i add the code which creates hp, hunger and thirst bar for my game, the game doesn't load on normal run (and it crashes), but it does fully load when i run the debugger and it works just fine.

    this is the code that i run in game.cpp:
    Qt Code:
    1. void Game::drawBars()
    2. {
    3. bars = new Bars();
    4. scene->addItem(bars->hp);
    5. scene->addItem(bars->hunger);
    6. scene->addItem(bars->thirst);
    7. }
    To copy to clipboard, switch view to plain text mode 

    This is Bars.h file
    Qt Code:
    1. #ifndef BARS_H
    2. #define BARS_H
    3.  
    4. #include <QGraphicsRectItem>
    5. #include <QGraphicsItem>
    6. #include <QObject>
    7.  
    8. class Bars: public QObject{
    9. Q_OBJECT
    10. public:
    11. Bars();
    12. QLineF line;
    13. QLineF dist;
    14. void setPosition();
    15. private:
    16. int hp_amount;
    17. int hunger_amount;
    18. int thirst_amount;
    19. float size;
    20. float hp_height;
    21. float hunger_height;
    22. float thirst_height;
    23. };
    24.  
    25. #endif // BARS_H
    To copy to clipboard, switch view to plain text mode 

    This is Bars.cpp file
    Qt Code:
    1. Bars::Bars()
    2. {
    3. hp_amount = 1400;
    4. hunger_amount = 1400;
    5. thirst_amount = 1400;
    6. QVector<QPointF> hp_points,hunger_points,thirst_points;
    7. hp_points << QPointF(1,0) << QPointF(2,0) << QPointF(2.33,1) << QPointF(2.66,2) << QPointF(3,3) << QPointF(3,4) << QPointF(2.66,5) << QPointF(2.33,6) << QPointF(2,7) << QPointF(1,7) << QPointF(0.66,6) << QPointF(0.33,5) << QPointF(0,4) << QPointF(0,3) << QPointF(0.33,2) << QPointF(0.66,1);
    8. hunger_points << QPointF(-1,0) << QPointF(0,0) << QPointF(-0.33,1) << QPointF(-0.66,2) << QPoint(-1,3) << QPoint(-1,4) << QPointF(-0.66,5) << QPointF(-0.33,6) << QPointF(0,7) << QPoint(-1,7) << QPointF(-1.33,6) << QPointF(-1.66,5) << QPoint(-2,4) << QPointF(-2,3) << QPointF(-1.66,2) << QPointF(-1.33,1);
    9. thirst_points << QPoint(3,0) << QPoint(4,0) << QPointF(4.33,1) << QPointF(4.66,2) << QPoint(5,3) << QPoint(5,4) << QPointF(4.66,5) << QPointF(4.33,6) << QPointF(4,7) << QPointF(3,7) << QPointF(3.33,6) << QPointF(3.66,5) << QPointF(4,4) << QPointF(4,3) << QPointF(3.66,2) << QPointF(3.33,1);
    10.  
    11. line.setPoints(hunger_points[0], hp_points[0]);
    12. dist.setPoints(hp_points[0], hp_points[1]);
    13. size = game->getViewHeight()/57;
    14.  
    15. for(int i=0;i<hp_points.size();i++){
    16. hp_points[i] *= size;
    17. hunger_points[i] *= size;
    18. thirst_points[i] *= size;
    19. }
    20.  
    21. hp = new QGraphicsPolygonItem(QPolygonF(hp_points));
    22. hunger = new QGraphicsPolygonItem(QPolygonF(hunger_points));
    23. thirst = new QGraphicsPolygonItem(QPolygonF(thirst_points));
    24.  
    25. hp_height = hp->boundingRect().height();
    26. hunger_height = hunger->boundingRect().height();
    27. thirst_height = thirst->boundingRect().height();
    28.  
    29. setPosition();
    30.  
    31. hp->setBrush(QBrush("#f44047"));
    32. hunger->setBrush(QBrush("#efbc12"));
    33. thirst->setBrush(QBrush("#00a8f3"));
    34. //hp->setPen(Qt::NoPen);
    35. //hunger->setPen(Qt::NoPen);
    36. //thirst->setPen(Qt::NoPen);
    37. hp->setZValue(16);
    38. }
    39.  
    40. void Bars::setPosition()
    41. {
    42. game->scene->removeItem(hp);
    43. game->scene->addItem(hp);
    44. game->scene->removeItem(hunger);
    45. game->scene->addItem(hunger);
    46. game->scene->removeItem(thirst);
    47. game->scene->addItem(thirst);
    48.  
    49. hp->setPos(game->hotbarSlots->slot[8]->x()+game->hotbarSlots->slot[8]->boundingRect().width()+(game->getViewWidth()*0.175)/2-dist.length()/2, game->hotbarSlots->slot[8]->y()+game->hotbarSlots->slot[8]->boundingRect().width()-hp_height);
    50. hunger->setPos(hp->x()-line.length()/2,game->hotbarSlots->slot[8]->y()+game->hotbarSlots->slot[8]->boundingRect().width()-hunger_height);
    51. thirst->setPos(hp->x()+line.length()/2,game->hotbarSlots->slot[8]->y()+game->hotbarSlots->slot[8]->boundingRect().width()-thirst_height);
    52. }
    To copy to clipboard, switch view to plain text mode 

    Any help or advice is appreciated !!!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Crashes on run, works on debugging

    Where is the variable "game" at line 13 of Bars.cpp defined and initialized? It is not a member variable and has not been passed in to the constructor but it is used extensively. If unused unintialized, or its member scene etc. has not been initialized, then segfaults will be your friend.

  3. #3
    Join Date
    Sep 2019
    Posts
    20
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Crashes on run, works on debugging

    i defined game class globally in main.cpp like this:
    Qt Code:
    1. #include "game.h"
    2.  
    3. #include <QApplication>
    4. #include <QTextCodec>
    5.  
    6. Game *game;
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QApplication a(argc, argv);
    11.  
    12. srand(time(0));
    13. game = new Game;
    14. game->show();
    15. return a.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

    And in the Bars.cpp its initialized like this:
    Qt Code:
    1. #include "game.h"
    2.  
    3. extern Game *game;
    To copy to clipboard, switch view to plain text mode 

    And if it helps here is the game.h file
    Qt Code:
    1. #ifndef GAME_H
    2. #define GAME_H
    3. #include "player.h"
    4. #include "button.h"
    5. #include "sea.h"
    6. #include "land.h"
    7. #include "hotbarslots.h"
    8. #include "soundeffects.h"
    9. #include "story.h"
    10. #include "rabbithole.h"
    11. #include "tree.h"
    12. #include "rock.h"
    13. #include "bars.h"
    14.  
    15. #include <QGraphicsView>
    16. #include <QGraphicsScene>
    17. #include <QObject>
    18. #include <QMouseEvent>
    19. #include <QGraphicsSceneMouseEvent>
    20. #include <QGraphicsScene>
    21. #include <QWidget>
    22. #include <QKeyEvent>
    23.  
    24. class Game: public QGraphicsView{
    25. Q_OBJECT
    26. public:
    27. // this class stuff
    28. static QString in_room;
    29. static QString day_or_night;
    30. static int language; // 0~ENG, 1~SLO,
    31. Game(QWidget *parent=NULL);
    32. void languageMenu();
    33. void menu();
    34. void createMap();
    35. void createPlayer();
    36. void createHotbarSlots();
    37. void spawnAnimals();
    38. void spawnTrees();
    39. void spawnRocks();
    40. void drawBars();
    41. void setStopRectCreated(bool t);
    42. bool getStopRectCreated();
    43. int getDesktopHeight();
    44. int getDesktopWidth();
    45. int getViewHeight();
    46. int getViewWidth();
    47. void setViewWidthToDesktop();
    48. void setViewHeightToDesktop();
    49. void setViewWidthToNormal();
    50. void setViewHeightToNormal();
    51. void changeColorOfButton();
    52. QGraphicsRectItem *stopRect;
    53. void wheelEvent(QWheelEvent *event);
    54. // dark screen stuff
    55. QGraphicsRectItem *darkScreen;
    56. QTimer *darkScreenTimer;
    57. void deleteDarkScreen();
    58. // other class stuff
    59. RabbitHole *rabbitHole;
    60. Story *story;
    61. Player *player;
    62. Land *land;
    63. Sea *sea;
    64. HotbarSlots *hotbarSlots;
    65. SoundEffects *soundEffects;
    66. Tree *tree[3];
    67. Rock *rock[3];
    68. Bars *bars;
    69. // static stuff
    70. private:
    71. // dark screen stuff
    72. double darking_opacity;
    73. bool dark_screen_on;
    74. // this class stuff
    75. int curr_pos_in_menu;
    76. bool stop_rect_created;
    77. int desktop_height;
    78. int desktop_width;
    79. int current_height;
    80. int current_width;
    81. int starting_width;
    82. int starting_height;
    83. public slots:
    84. void setLanguageToSlovenian();
    85. void setLanguageToEnglish();
    86. void startStory();
    87. void start();
    88. // dark screen stuff
    89. void screenGoesDark();
    90. void screenComesFromDark();
    91. };
    92.  
    93. #endif // GAME_H
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,243
    Thanks
    303
    Thanked 866 Times in 853 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Crashes on run, works on debugging

    There is a big difference between code running in the debugger vs. code running in release mode. To help you debug, the debugger will initialize variables and pointers to known but invalid values. Usually when you try to access something using one of these variables or pointers, it will result in a crash and you can use the debugger to find out where and why. So if your program doesn't crash in debug mode, then it is probably purely by accident that the debugger's initialization lets it run correctly (maybe - probably at some point it will crash - you just haven't tested that part of the code yet).

    In release mode, nothing is initialized. Pointers and variables have whatever random values your program memory has when it starts up. So if you haven't initialized something yourself, if you have mistakenly "double declared" a pointer or other variable (like as a class member, then again as a local variable in a method, hiding the class member), if you access a position in a vector or array that is out of bounds, any of these could cause a crash.

    In your code, you do no error checking at all that I can see. It just assumes that every pointer is valid, every array index is within bounds, and every function call will work and return a valid result. You have calls with three or four levels of pointer indirection (a->b->c->d) and your code assumes every one of those will result in a valid pointer.

    So the error could be anywhere. The first thing you should do is to initialize EVERY variable, either where it is declared (Game * game = nullptr or in class constructors, or in methods where local variables are created. Then pay attention to compiler warnings. If you see a warning that a variable is being used before it is assigned a value, hint, hint... And when you run the code in the debugger, if you have initialized all of your variables yourself instead of relying on the debugger to do it, your program should crash and you can find out what is wrong. The debugger doesn't just crash and exit, it tells you what went wrong (index out of range in an array, invalid pointer, etc.). Pay attention to that.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Jun 2012
    Location
    Austria
    Posts
    22
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Crashes on run, works on debugging

    Fully agree with d_stranz - initializing data is your friend :-)
    But: Debug-code is pre-initilized to fixed but to - for your application - usually meaningless values. If you have an uninitialized pointer it doesn't make much difference if it is a true null pointer or some garbage, pointing anywhere. In both cases your program will usually crash. (Since you do not check for null pointers...)
    So for fast fixing your problem I would focus on data structures which contain array sizes, indices pointing to elements in containers and so on. If these are zero-ed your code may work but if they index something outside your container the program will crash.
    Good luck and work on your coding style :-)

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Crashes on run, works on debugging

    Everything above is good advice IMHO.

    I cannot see all your code but I suspect...
    At line 13 of main.cpp you create a new Game object.
    During constructing the Game object you more than likely try to create a Bar object (in drawBars()).
    Constructing the Bar object relies on the global Game object pointer which has not been initialised yet (because you are still evaluating the RHS of line 13).


    You need to rethink how objects get a pointer to the Game object, and whether it is needed at all. Why does the Bars object need to know about the Game object? Is there a better way to achieve the same thing?

Similar Threads

  1. Extending QApplication: Debug crashes, Release works
    By LinuCC in forum Qt Programming
    Replies: 2
    Last Post: 31st October 2014, 01:29
  2. Replies: 15
    Last Post: 9th November 2012, 20:07
  3. It works once, but crashes if I do it again
    By Astrognome in forum Newbie
    Replies: 3
    Last Post: 28th March 2012, 01:06
  4. QT 4.7.1 Win 7 x64 VS 2010: Debug works, Release crashes
    By new_voodoo in forum Qt Programming
    Replies: 2
    Last Post: 28th November 2010, 20:14
  5. Replies: 1
    Last Post: 30th March 2009, 23:25

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.