SFML community forums

Help => General => Topic started by: Pein95 on August 30, 2013, 07:22:20 pm

Title: Unhandled exception at 0x77AB22C2 (ntdll.dll)
Post by: Pein95 on August 30, 2013, 07:22:20 pm
//Game.h
#pragma once
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

class Game
{
public:
        static void Start();

private:
        static bool IsExiting();
        static void GameLoop();

        enum GameState {Uninitialized, ShowingSplash, Paused,
                ShowingMenu, Playing, Exiting};

        static GameState _gameState;
        static sf::RenderWindow _mainWindow;
};
 

#include "Game.h"


Game::GameState Game::_gameState = Uninitialized;
sf::RenderWindow Game::_mainWindow;


void Game::Start()
{
        if (_gameState != Uninitialized)
                return;

        _mainWindow.create(sf::VideoMode(1024, 768,32), "Pang!");
        _gameState = Game::Playing;

        while (!IsExiting())
        {
                GameLoop();
        }

        _mainWindow.close();
}


bool Game::IsExiting()
{
        if (_gameState == Game::Exiting)
                return true;
        else
                return false;
}

void Game::GameLoop()
{
        sf::Event currentEvent;
        while(_mainWindow.pollEvent(currentEvent))
        {
                switch (_gameState)
                {
                case Game::Playing:
                        {
                                _mainWindow.clear(sf::Color(255,0,0));
                                _mainWindow.display();

                                if(currentEvent.type == sf::Event::Closed)
                                {
                                        _gameState = Game::Exiting;
                                }
                                break;
                        }
                       
                       

                }
        }
}


 

I have error in this line: sf::RenderWindow Game::_mainWindow;.
How I can fix it?
Sorry for my English) it is not my native English)
Title: Re: Unhandled exception at 0x77AB22C2 (ntdll.dll)
Post by: eXpl0it3r on August 30, 2013, 07:34:18 pm
Don't use static (= global) variables, especially not for SFML objects like sf::RenderWindow.
Title: Re: Unhandled exception at 0x77AB22C2 (ntdll.dll)
Post by: Pein95 on August 30, 2013, 07:39:05 pm
Thanks)
Title: Re: Unhandled exception at 0x77AB22C2 (ntdll.dll)
Post by: cmbrgames on August 30, 2013, 09:45:30 pm
I've got the same error when I try to run my project compiled in Debug. In Release it works fine. I'm using Visual Studio 2012.

Don't use static (= global) variables, especially not for SFML objects like sf::RenderWindow.

So is this code below wrong when using SFML? I use this to share textures between different types of entities in my game and to load them only once at the start of the game for each type of entity.

Code: [Select]
class Monster : public Thing
{
private:
    static std::vector<sf::Texture> textures;
public:
    static bool loadTextures();
}
Title: Re: Unhandled exception at 0x77AB22C2 (ntdll.dll)
Post by: Jove on August 31, 2013, 01:03:53 am
Pein95, out of interest are you using SFML 2.1?
Title: Re: Unhandled exception at 0x77AB22C2 (ntdll.dll)
Post by: eXpl0it3r on August 31, 2013, 01:24:43 am
So is this code below wrong when using SFML? I use this to share textures between different types of entities in my game and to load them only once at the start of the game for each type of entity.

class Monster : public Thing
{
private:
    static std::vector<sf::Texture> textures;
public:
    static bool loadTextures();
}
 
Short answer: Yes

Longer answer: There are two factors:
The issue specifically with SFML is, that SFML itself initializes stuff globally on its own and since the order of destruction in the global scope is undefined, one will run into crashes when the random order doesn't line up as imagined. So one has to make sure, that everything gets instantiated and destroyed in a local scope.

The other factor is a discussion on code design and this has comes always with some controversy. For me and many other people with orientation for modern design, global variables, which include static class member, are in most cases a bad design.
Title: Re: Unhandled exception at 0x77AB22C2 (ntdll.dll)
Post by: Nexus on August 31, 2013, 01:34:45 am
A cleaner design would be a separate class that has the responsibility of managing the textures. This can be a simple wrapper around std::vector<sf::Texture>, or something more elaborate like the ResourceHolder (https://github.com/SFML/SFML-Game-Development-Book/blob/master/02_Resources/Include/Book/ResourceHolder.hpp) with a std::map we used in the book.

The advantages are clearly-defined lifetimes (you know when the resources are constructed and destroyed), separate responsibilities (Monster needn't care about correct loading, it can simply request a texture) and reusability (you can use the texture loading functionality for other classes than Monster).
Title: Re: Unhandled exception at 0x77AB22C2 (ntdll.dll)
Post by: cmbrgames on September 01, 2013, 03:55:48 pm
Thanks. I'll try to remove the static stuff from my game and see if it works in Debug. The ResourceHolder using std::map is a good idea, because at the moment I have to worry about loading them in correct order and using index numbers.

[edit]
Removing static stuff fixed it and now it works in Debug mode too. Also it seemed to fix another problem with slowly rising memory usage. My game would initially use up 40MB and rise even up to 150MB for some reason, now it seems to keep the usage steady around ~37MB.