Results 1 to 4 of 4

Thread: Program crashes with 0xC0000005 error code

  1. #1
    Join Date
    Apr 2021
    Posts
    6
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Program crashes with 0xC0000005 error code

    Hello everyone,

    I have made small computer game using QT in CLion.
    At beginning I show main menu with four rect items created as buttons, I connect it to some slots f.e start() - slot which start game, showHelp() - showing help informations, showScores() - top scores and the last one is quit connected to close() slot.

    Let me explain how my code works - I running for first time application then all items in the main menu working. When I click the start button it bring me to start method and all code from this method working as I wish.
    If the player health is equal 0 or lower program showing gameOverWindow() and in this window have two buttons. One is playAgain and this button restarting game, next one moving to main menu - this menu which are displayed at start of my program.

    The problem that arises is when I click play again button it successful restart game, but sometimes it crashing my application at showGameOverWindow or after when I back to mainMenuWindow with those error information:
    pure virtual method called terminate called without an active exception
    but not always, sometimes it's this exit code:
    Process finished with exit code -1073741819 (0xC0000005)
    When it doesn't crash after that cases program will always crash after clicking any button in mainMenu without play game button.

    Any help would be appreciated

    Source code:

    Code which checking health is under or equal 0
    Qt Code:
    1. //some useless code before
    2. if (game->health->getHealth() <= 0){
    3. int score = game->score->getScore();
    4.  
    5. game->scene->clear();
    6. game->ShowGameOverWindow(score);
    7. }
    To copy to clipboard, switch view to plain text mode 

    ShowGameOverWindow
    Qt Code:
    1. void Game::ShowGameOverWindow(int score){
    2. setBackgroundBrush(QImage("../Sources/Pictures/gameover.png"));
    3. float height = window()->height();
    4. float width = window()->width();
    5.  
    6. drawPanel(0,0,860,600,Qt::black,0.35);
    7. drawPanel(width/4+30,height/4,400,400,Qt::lightGray,0.75);
    8.  
    9. backMenuButton = new Button(QString("../Sources/Pictures/Menu/ok-inactive.png"),
    10. QString("../Sources/Pictures/Menu/ok-active.png"));
    11. int qxPos = width-320;
    12. int qyPos = height-110;
    13. backMenuButton->setPos(qxPos, qyPos);
    14. connect(backMenuButton, SIGNAL(clicked()), this, SLOT(displayMainMenu()));
    15. scene->addItem(backMenuButton);
    16.  
    17. playAgainButton = new Button(QString("../Sources/Pictures/Menu/again-inactive.png"),
    18. QString("../Sources/Pictures/Menu/again-active.png"));
    19. int pxPos = width/3-30;
    20. int pyPos = height-110;
    21. playAgainButton->setPos(pxPos, pyPos);
    22. playAgainButton->setSize(200,51);
    23. connect(playAgainButton,SIGNAL(clicked()), this, SLOT(start()));
    24. scene->addItem(playAgainButton);
    25. }
    To copy to clipboard, switch view to plain text mode 

    start game slot
    Qt Code:
    1. void Game::start() {
    2. scene->clear();
    3.  
    4. scene->setSceneRect(0,0,800,600);
    5. setFixedSize(800,600);
    6. setBackgroundBrush(QImage("../Sources/Pictures/gameBackground.png"));
    7.  
    8. player = new Player();
    9. player->setFlag(QGraphicsItem::ItemIsFocusable);
    10. player->setFocus();
    11. player->resetPos();
    12. scene->addItem(player);
    13.  
    14. score = new Score();
    15. score->resetScore();
    16. score->setPos(score->x()+600, score->y()+9);
    17. scene->addItem(score);
    18.  
    19. health = new Health();
    20. health->resetHealth();
    21. health->setPos(health->x()+380, health->y()+9);
    22. scene->addItem(health);
    23.  
    24. if(!mainTimer->isActive()) {
    25. connect(mainTimer, SIGNAL(timeout()), this, SLOT(mainLoop()));
    26. mainTimer->start(0);
    27. }
    28. counting(1000);
    29.  
    30. auto * timerHurdle = new QTimer();
    31. QObject::connect(timerHurdle,SIGNAL(timeout()),player,SLOT(spawnHurdle()));
    32. timerHurdle->start(2000);
    33.  
    34. auto * timerHeart = new QTimer();
    35. QObject::connect(timerHeart,SIGNAL(timeout()),player,SLOT(spawnHeart()));
    36. timerHeart->start(10000);
    37. }
    To copy to clipboard, switch view to plain text mode 

    Display main menu
    Qt Code:
    1. void Game::displayMainMenu() {
    2. scene->clear();
    3.  
    4. scene->setSceneRect(0,0,1030,768);
    5. setFixedSize(1030,768);
    6. setBackgroundBrush(QImage("../Sources/Pictures/Menu/background.png"));
    7.  
    8. // buttons and properties
    9. playButton = new Button(QString("../Sources/Pictures/Menu/start-inactive.png"),
    10. QString("../Sources/Pictures/Menu/start-active.png"));
    11. int bxPos = playButton->boundingRect().width()/8 + 3;
    12. int byPos = 380;
    13. playButton->setPos(bxPos, byPos);
    14. connect(playButton,SIGNAL(clicked()), this, SLOT(start()));
    15. scene->addItem(playButton);
    16.  
    17. scoreButton = new Button(QString("../Sources/Pictures/Menu/scores-inactive.png"),
    18. QString("../Sources/Pictures/Menu/scores-active.png"));
    19. int sxPos = scoreButton->boundingRect().width()/8 + 3;
    20. int syPos = 450;
    21. scoreButton->setPos(sxPos, syPos);
    22. connect(scoreButton,SIGNAL(clicked()), this, SLOT(showScores()));
    23. scene->addItem(scoreButton);
    24.  
    25. helpButton = new Button(QString("../Sources/Pictures/Menu/help-inactive.png"),
    26. QString("../Sources/Pictures/Menu/help-active.png"));
    27. int hxPos = helpButton->boundingRect().width()/8 + 3;
    28. int hyPos = 520;
    29. helpButton->setPos(hxPos, hyPos);
    30. connect(helpButton,SIGNAL(clicked()), this, SLOT(showHelp()));
    31. scene->addItem(helpButton);
    32.  
    33.  
    34. quitButton = new Button(QString("../Sources/Pictures/Menu/quit-inactive.png"),
    35. QString("../Sources/Pictures/Menu/quit-active.png"));
    36. int qxPos = quitButton->boundingRect().width()/8 + 3;
    37. int qyPos = 610;
    38. quitButton->setPos(qxPos, qyPos);
    39. connect(quitButton,SIGNAL(clicked()), this, SLOT(close()));
    40. scene->addItem(quitButton);
    41.  
    42. backButton = new Button(QString("../Sources/Pictures/Menu/back-inactive.png"),
    43. QString("../Sources/Pictures/Menu/back-active.png"));
    44. int backxPos = scene->width()/2 - 40;
    45. int backyPos = 610;
    46. backButton->setPos(backxPos, backyPos);
    47. connect(backButton,SIGNAL(clicked()), this, SLOT(displayMainMenu()));
    48. scene->addItem(backButton);
    49.  
    50. parchmentImage = new QLabel();
    51. QPixmap img("../Sources/Pictures/Menu/paper.png");
    52. parchmentImage->setPixmap(img);
    53.  
    54.  
    55. double x = img.width();
    56. double y = img.height();
    57. parchmentImage->setGeometry(300,250,x,y);
    58. scene->addWidget(parchmentImage);
    59.  
    60. info = new TextInformation();
    61. info->setPosition(300,270);
    62. scene->addItem(info);
    63.  
    64.  
    65. if(backButton->isVisible()) { scene->removeItem(backButton); }
    66. if(info->isVisible()) { scene->removeItem(info); }
    67.  
    68.  
    69. info->setProperties(Qt::black,"arial",16,300,270);
    70. parchmentImage->setHidden(true);
    71.  
    72.  
    73. playButton->setEnabled(true);
    74. scoreButton->setEnabled(true);
    75. helpButton->setEnabled(true);
    76. quitButton->setEnabled(true);
    77.  
    78. }
    To copy to clipboard, switch view to plain text mode 


    If the problem is too hard to imagine I can make short video to show you how I really looks or even paste more source code.
    Last edited by nemaider; 30th May 2021 at 23:14.

  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: Program crashes with 0xC0000005 error code

    You are almost certainly attempting to access something through a pointer that is no longer valid. Start the program in your debugger and, when it crashes, obtain the stack backtrace. Reading the trace from the top, the first line of your code (i.e. outside the Qt library) is the place to start looking.

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Program crashes with 0xC0000005 error code

    When are you calling the methods you have listed above? Every time you start a new game or show the "game over" window? If these functions are being called multiple times, then it seems to me that you have a serious logic problem: it looks like you are creating member variables multiple times instead of creating them once (in a constructor or some other "initialize" method that is called once) and re-using them. Every time you call new() to assign a member variable, the previous value assigned to that variable is lost. At best you have a memory leak, at worst you could be using a copy of a pointer that no longer points to the newly-created member variable.

    You also haven't shown any code where you define and assign initial values to your member variables. An unassigned pointer variable can easily cause a crash if you try to use it before it is assigned (or after it is deleted).

    As ChrisW67 says, create a debug version of you program and run it in the debugger until it crashes. The stack trace will show you where. Also when compiling your program do not ignore compiler warnings about unassigned variables or other potential problems.
    <=== 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.

  4. #4
    Join Date
    Apr 2021
    Posts
    6
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Program crashes with 0xC0000005 error code

    Okey, I got it, thank you so much.
    I run program in the debugger and bug was in mainLoop function. This function was called all the time when timer was connected into this slot method. I fixed it by disconnecting object from any signals, and this working.

    Sorry, for my topic. I run the debugger earlier but I don't see any problem with this function where the debugger bring me.

Similar Threads

  1. Qt program "exited with code 1994410319" error?
    By evanw1256 in forum Qt Programming
    Replies: 4
    Last Post: 20th February 2014, 15:38
  2. 0xc0000005 error when using qVector
    By przemo in forum Newbie
    Replies: 5
    Last Post: 9th December 2010, 15:02
  3. Program crashes
    By Fallen_ in forum Qt Programming
    Replies: 49
    Last Post: 20th September 2010, 02:41
  4. Replies: 6
    Last Post: 17th March 2010, 11:53
  5. Program crashes with assert error in xcb_lock.c
    By Valheru in forum Qt Programming
    Replies: 3
    Last Post: 18th November 2007, 20:56

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.