SFML community forums

Help => Window => Topic started by: LapertuS on June 14, 2016, 01:34:08 pm

Title: 0xC0000005: Access violation reading location, when closing SFML window
Post by: LapertuS on June 14, 2016, 01:34:08 pm
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;}

}

Title: AW: 0xC0000005: Access violation reading location, when closing SFML window
Post by: eXpl0it3r on June 14, 2016, 01:49:39 pm
What's the stack trace?
Title: Re: 0xC0000005: Access violation reading location, when closing SFML window
Post by: LapertuS on June 14, 2016, 02:08:53 pm
        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]
 
Title: Re: 0xC0000005: Access violation reading location, when closing SFML window
Post by: Laurent on June 14, 2016, 02:29:12 pm
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?
Title: Re: 0xC0000005: Access violation reading location, when closing SFML window
Post by: LapertuS on June 14, 2016, 02:39:13 pm
Yeah completely missed that, this is fixed now, but still same error occurs.
Title: Re: 0xC0000005: Access violation reading location, when closing SFML window
Post by: texus on June 15, 2016, 07:13:38 pm
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.