Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: 0xC0000005: Access violation reading location, when closing SFML window  (Read 5897 times)

0 Members and 1 Guest are viewing this topic.

LapertuS

  • Newbie
  • *
  • Posts: 3
    • View Profile
I'm building a game and currently i'm working on GUI. Problem is that, when I use sf::RenderWindow.close() inside callback, which is called when I click on GUI button, then program crashes and Visual Studio gives me following errors. I'm using TGUI library.

First-chance exception at 0x00007FFC53F57CF3 (sfml-window-d-2.dll) in Poker.exe: 0xC0000005: Access violation reading location 0x0000009900000113.
Unhandled exception at 0x00007FFC53F57CF3 (sfml-window-d-2.dll) in Poker.exe: 0xC0000005: Access violation reading location 0x0000009900000113.

GUI_Menu.cpp, where I call window.close()

#include "GUI_Menu.h"

void GUI_Menu::InitalizeMenuWidgets()
{
        gui.setWindow(this->window);
       
        this->playButton = std::make_shared<tgui::Button>();
        this->quitButton = std::make_shared<tgui::Button>();

        auto _width = tgui::bindWidth(this->gui);
        auto _height = tgui::bindHeight(this->gui);

        auto _buttonHeight = _height / 9;
        auto _buttonWidth = _width / 2;

        this->playButton->setSize(_buttonWidth, _buttonHeight);
        this->quitButton->setSize(_buttonWidth, _buttonHeight);

        this->playButton->setPosition(_width/4, _height * 1.5/10);
        this->quitButton->setPosition(_width / 4, _height * 3/10);
       

        this->playButton->setText("Play");
        this->quitButton->setText("Quit");

        this->quitButton->connect("pressed", &GUI_Menu::function, this);
        gui.add(playButton);
        gui.add(quitButton);

       

}

void GUI_Menu::function() {
        std::cout << "DEBUG";
        this->window.close();
}

 

GUI_Menu.h
#ifndef GUI_MENU_H
#define GUI_MENU_H


#include "GUI.h"
#include <iostream>

class GUI_Menu
{
       
        private:
                void function();

                sf::RenderWindow &window;
                tgui::Gui &gui;
                tgui::Button::Ptr playButton;
                tgui::Button::Ptr quitButton;


        public:
                GUI_Menu(sf::RenderWindow &window, tgui::Gui &gui) : window(window), gui(gui){ InitalizeMenuWidgets(); }
               
                void InitalizeMenuWidgets();
       
};
               

#endif

game.cpp, where my game loop runs.
#include "game.h"


Game::Game()
{
        //Initalization
        window.create(sf::VideoMode(700, 400), "Poker Game");
        gui.setWindow(window);

        GUI_Menu(this->window, this->gui);
       
        this->isFullScreen = true;

}



void Game::GameLoop()
{
        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        switch (event.type)
                        {
                                case sf::Event::KeyReleased:
                                        switch (event.key.code)
                                                case sf::Keyboard::Return:
                                                        this->ToggleFullScreen();
                                        break;


                                case sf::Event::Closed:
                                        window.close();
                                        break;
                               
                                       
                        }
                       
                        gui.handleEvent(event);
                }
                window.clear();
                gui.draw();
                window.display();

        }
}

void Game::ToggleFullScreen()
{
        if (this->isFullScreen){
                window.create(sf::VideoMode(700, 400), "Poker Game", sf::Style::Default);
                this->isFullScreen = false;}
       
       
        else{
                window.create(sf::VideoMode(1920, 1080), "Poker Game", sf::Style::Fullscreen);
                this->isFullScreen = true;}

}


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
What's the stack trace?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

LapertuS

  • Newbie
  • *
  • Posts: 3
    • View Profile
        sfml-window-d-2.dll!00007ff8eba77cf3()  Unknown
>       Poker.exe!GUI_Menu::function() Line 40  C++
        [External Code]
        tgui-d.dll!tgui::Signal::operator()(unsigned int __formal) Line 77      C++
        tgui-d.dll!tgui::Signal::operator()<sf::String>(unsigned int count, const sf::String & value) Line 262  C++
        tgui-d.dll!tgui::SignalWidgetBase::sendSignal<sf::String>(std::basic_string<char,std::char_traits<char>,std::allocator<char> > && name, sf::String <args_0>) Line 502   C++
        tgui-d.dll!tgui::Button::leftMouseReleased(float x, float y) Line 216   C++
        tgui-d.dll!tgui::Container::handleEvent(sf::Event & event) Line 741     C++
        tgui-d.dll!tgui::Gui::handleEvent(sf::Event event) Line 206     C++
        Poker.exe!Game::GameLoop() Line 42      C++
        Poker.exe!main() Line 7 C++
        [External Code]
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Quote
GUI_Menu(this->window, this->gui);
This line looks really wrong. What's the purpose of calling a constructor without defining a named instance?
Laurent Gomila - SFML developer

LapertuS

  • Newbie
  • *
  • Posts: 3
    • View Profile
Yeah completely missed that, this is fixed now, but still same error occurs.

texus

  • Sr. Member
  • ****
  • Posts: 499
    • View Profile
    • TGUI
    • Email
If the GUI_Menu object no longer exists when the callback occurs (which was the case when you didn't assign it to a named instance) then the function tries to access the this->window reference of an already destructed instance and will cause the crash.

If that is not the problem then you might want to minimize the code so that it fits in one file such that I can just copy-paste it here to test it.
TGUI: C++ SFML GUI

 

anything