SFML community forums

Help => Window => Topic started by: Mysquff on May 29, 2016, 11:53:29 am

Title: Application crashes when declaring sf::Window outside a function in debug mode
Post by: Mysquff on May 29, 2016, 11:53:29 am
I was trying to create a simple game and got stuck at the beginning. The app builds correctly and when launched throws exception with a message:
Quote
Exception thrown at 0x775F9F83 (ntdll.dll) in pong.exe: 0xC0000005: Access violation writing location 0x00000004.
Visual Studio Debugger points me at the line 6 in Window.cpp file ("This is the next statement to execute when this thread returns from the current function").

Apart from that I get IntelliSense errors only:
Quote
no instance of constructor "sf::ContextSettings::ContextSettings" matches the argument list pong Window.hpp 89
Quote
no instance of constructor "sf::ContextSettings::ContextSettings" matches the argument list pong Window.hpp 106
Quote
no instance of constructor "sf::ContextSettings::ContextSettings" matches the argument list pong Window.hpp 133
Quote
no instance of constructor "sf::ContextSettings::ContextSettings" matches the argument list pong Window.hpp 151
I searched for solution here and at StackExchange and I discovered it's not advisable to declare sf::Window as global. I don't do this, but it seems simply declaring it outside a function causes problems. Still I'd really like to have this object as Window static class member when the project grows bigger.

I remember a year ago on some earlier version on SFML I was able to declare it in such way without any problems.

Problem occurs in debug mode only. Program executes just fine in Release mode.

Could you help me? I don't really know what I am doing wrong. Here are all project files at the moment.

main.cpp

#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Network.hpp>
#include "App.h"
#include "Window.h"

int main()
{
    App::run();


    return 0;
}

App.h
class App
{
private:
    static void     loop();

public:
    static void     run();
};

App.cpp
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Network.hpp>
#include "Window.h"
#include "App.h"

void App::run()
{
    Window::initialize();

    loop();
}

void App::loop()
{
    while (Window::window.isOpen())
    {
        sf::Event event;
        while (Window::window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                Window::window.close();
        }
    }
}

Window.h
class Window
{

public:
    static void initialize();
    static sf::Window window;
};

Window.cpp
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Network.hpp>
#include "Window.h"

sf::Window Window::window; // <--- This is the next statement to execute when this thread returns from the current function

void Window::initialize()
{
    window.create(sf::VideoMode::getDesktopMode(), "Pong");
}
Title: AW: Application crashes when declaring sf::Window outside a function in debug mode
Post by: eXpl0it3r on May 29, 2016, 11:59:46 am
Declaring a window static is pretty much the same as declaring it as a global.
Title: Re: Application crashes when declaring sf::Window outside a function in debug mode
Post by: Mysquff on May 29, 2016, 12:02:41 pm
I see. What do you advise me to do? Should I create a class instance of Window and make a non-static sf::Window member of it? 
Title: AW: Application crashes when declaring sf::Window outside a function in debug mode
Post by: eXpl0it3r on May 29, 2016, 02:16:11 pm
What do you gain of having a "static" App class anyways?
Just remove the static keywords, put the initialization method into the constructor of the class (that's why they exist anyways), create an instance of your App class in the main function and call run on it.

That way the window becomes scope within the class and the initialization gets properly done in the cobstructor and won't be forgotten to call by accident. ;)
Title: Re: Application crashes when declaring sf::Window outside a function in debug mode
Post by: Mysquff on May 29, 2016, 04:12:16 pm
Thanks for help and the suggestion. I wanted to keep my code as easy to read and understand, and I decided this was the best way to achieve this. It seems I have to rethink my approach and get rid of static classes.
Title: Re: Application crashes when declaring sf::Window outside a function in debug mode
Post by: Hapax on June 02, 2016, 12:59:21 am
Here's a simple example of a possibility of Game class implementation in action:
https://github.com/Hapaxia/MyPracticeBeginnerGames/blob/master/Puzza/main.cpp#L10-L11
https://github.com/Hapaxia/MyPracticeBeginnerGames/blob/master/Puzza/Game.hpp
https://github.com/Hapaxia/MyPracticeBeginnerGames/blob/master/Puzza/Game.cpp