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

Author Topic: [SOLVED] Static RenderWindow Access Vialation  (Read 1874 times)

0 Members and 1 Guest are viewing this topic.

DashWave

  • Newbie
  • *
  • Posts: 9
    • View Profile
[SOLVED] Static RenderWindow Access Vialation
« 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
« Last Edit: September 07, 2013, 07:47:14 pm by DashWave »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Static RenderWindow Access Vialation
« Reply #1 on: September 07, 2013, 07:39:19 pm »
Don't use global SFML variables, especially those who own an OpenGL context.
Laurent Gomila - SFML developer

DashWave

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Static RenderWindow Access Vialation
« Reply #2 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.