SFML community forums

Help => Graphics => Topic started by: DashWave on September 07, 2013, 07:19:37 pm

Title: [SOLVED] Static RenderWindow Access Vialation
Post by: DashWave on September 07, 2013, 07:19:37 pm
Hey,

I've been using SFML to create a game. I've decided to make the main game class completely static because passing around parent classes in C++ is somewhat messy. This does, of course, mean that all classes have access to the game class even if they don't deserve it but I'm willing to make that sacrifice.

When I tried to add a static RenderWindow to the class, however, I started getting access violations at 0x00000004 on the initializer line in the implementation of the class.

Here's the code necessary to reproduce the problem:
StaticClass.h:
#include <SFML/Graphics.hpp>

class StaticClass
{
public:
        static sf::RenderWindow renderWindow;
};
StaticClass.cpp:
#include "StaticClass.h"

sf::RenderWindow StaticClass::renderWindow;

Is this a bug? I don't get an access violation if I replace the class of the static member variable; I changed it to sf::Text and everything worked.

Thanks.

Edit: I'm using SFML 2.1
Title: Re: Static RenderWindow Access Vialation
Post by: Laurent on September 07, 2013, 07:39:19 pm
Don't use global SFML variables, especially those who own an OpenGL context.
Title: Re: Static RenderWindow Access Vialation
Post by: DashWave on September 07, 2013, 07:46:52 pm
Thanks for responding so quickly! I should have realised that's a necessary requirement considering what's happening under the hood.