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

Author Topic: Application crashes when declaring sf::Window outside a function in debug mode  (Read 2076 times)

0 Members and 1 Guest are viewing this topic.

Mysquff

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
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");
}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Declaring a window static is pretty much the same as declaring it as a global.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Mysquff

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
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? 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Mysquff

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
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.

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything