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

Author Topic: Unhandled exception on window creation  (Read 5370 times)

0 Members and 1 Guest are viewing this topic.

robvleugel

  • Newbie
  • *
  • Posts: 29
    • View Profile
Unhandled exception on window creation
« on: February 28, 2012, 07:12:34 pm »
[Using the latest 2.0 snapshot]

I'm not sure what I'm doing wrong here.

It compiles fine, but when running in debug mode I get:
"Unhandled exception at 0x77a322b2 in myprogram.exe: 0xC0000005: Access violation writing location 0x00000004."

It breaks at the GraphicsManager::GraphicsManager() constructor.
If I take out all code from the GraphicsManager class and put it all in main.cpp I get no errors.


Main.cpp
Code: [Select]


#include "GraphicsManager.h"

GraphicsManager gfx;

int main(int argc, char**argv)
{

gfx.Init();

return 0;
}


GraphicsManager.h
Code: [Select]


#include <SFML\Graphics.hpp>

class GraphicsManager
{
private:
int m_screen_x,m_screen_y,m_bit;
bool m_fullscreen
std::string m_winCaption;

sf::RenderWindow m_app;

public:
GraphicsManager();
~GraphicsManager() {m_app.Close();}

void Init();

};


GraphicsManager.cpp
Code: [Select]

#include "GraphicsManager.h"


GraphicsManager::GraphicsManager()
:m_fullscreen(false),m_screen_x(0),m_screen_y(0),m_bit(0),m_winCaption("not set")
{

}
void GraphicsManager::Init()
{
sf::VideoMode DesktopMode = sf::VideoMode::GetDesktopMode();
m_bit = DesktopMode.GetDesktopMode().BitsPerPixel;
m_screen_x = DesktopMode.GetDesktopMode().Width;
m_screen_y = DesktopMode.GetDesktopMode().Height;
m_winCaption = "MyProgram";
m_fullscreen = true;

m_app.Create(sf::VideoMode(m_screen_x, m_screen_y, m_bit), m_winCaption, sf::Style::Fullscreen);

}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Unhandled exception on window creation
« Reply #1 on: February 29, 2012, 07:57:52 am »
Your graphics manager is a global object, which is bad.
Then it creates a sf::RenderWindow in its constructor, which is even worse ;)

Do this once you're inside the main() function, and everything should be fine.
Laurent Gomila - SFML developer

robvleugel

  • Newbie
  • *
  • Posts: 29
    • View Profile
Unhandled exception on window creation
« Reply #2 on: February 29, 2012, 07:02:47 pm »
Making m_app a pointer to a sf::RenderWindow instead, and moving "GraphicsManager gfx;" to main() still doesn't solve my problem. Or isn't that what you meant?

This code still gives me the error:


Code: [Select]

#include "GameEngine\gfx\GraphicsManager.h"

class GraphicsManager
{
public:
sf::RenderWindow* m_app;
GraphicsManager() : m_app(nullptr) {}
};

int main(int argc, char**argv)
{
GraphicsManager gfx;
gfx.m_app->Create(sf::VideoMode(50, 50, 32), "blabla", sf::Style::Fullscreen);

return 0;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Unhandled exception on window creation
« Reply #3 on: February 29, 2012, 08:59:28 pm »
Your m_app pointer doesn't point to a valid RenderWindow, you must allocate it first.
Laurent Gomila - SFML developer

robvleugel

  • Newbie
  • *
  • Posts: 29
    • View Profile
Unhandled exception on window creation
« Reply #4 on: February 29, 2012, 10:44:33 pm »
Quote from: "Laurent"
Your m_app pointer doesn't point to a valid RenderWindow, you must allocate it first.


Eh.. yeah, stupid.

It seems I had two problems, I also had a similar case with an sf::Texture and sf::Sprite inside a class causing trouble. This made it a bit harder to track down.

So the lesson I learned is to never use static SFML objects, always use pointers and dynamic objects! :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Unhandled exception on window creation
« Reply #5 on: March 01, 2012, 08:06:21 am »
Quote
So the lesson I learned is to never use static SFML objects, always use pointers and dynamic objects!

Wrong. Never use pointers and dynamically allocated objects, unless you really need to.
The lesson you learnt is to never use global objects ;)
Laurent Gomila - SFML developer

robvleugel

  • Newbie
  • *
  • Posts: 29
    • View Profile
Unhandled exception on window creation
« Reply #6 on: March 01, 2012, 09:02:05 pm »
Quote from: "Laurent"
Quote
So the lesson I learned is to never use static SFML objects, always use pointers and dynamic objects!

Wrong. Never use pointers and dynamically allocated objects, unless you really need to.
The lesson you learnt is to never use global objects ;)


I have some problems occurring only with static SFML objects, I don't have this when I change the sf::Texture and sf::Sprite to pointers instead :) It are just class variables, no globals or anything. The class is a singleton, perhaps that causes the problem?


The code below runs fine, but when I change the Texture and Sprite pointers to real objects, I get trouble!

GS_Menu.h
Code: [Select]

#include "IGameState.h"
#include <SFML\Graphics.hpp>

class GameView;

class GS_Menu : public IGameState
{
public:

bool Init(GameView* gameView, GameEngine* gameEngine, GameLogic* gameLogic);

void DrawBackground();

~GS_Menu();

static GS_Menu* Instance() {
return &m_menuState;
}

protected:
GS_Menu();

private:
static GS_Menu m_menuState;
sf::Texture* m_bg;
sf::Sprite* m_bgs;
bool m_init;
};


GS_Menu.cpp
Code: [Select]

#include "GS_Menu.h"
#include "..\..\GameEngine\GameEngine.h"
#include <SFML\Graphics.hpp>


GS_Menu GS_Menu::m_menuState;

GS_Menu::GS_Menu(){}
GS_Menu::~GS_Menu(){}

bool GS_Menu::Init(GameView* gameView, GameEngine* gameEngine, GameLogic* gameLogic)
{
GView = gameView;
GEngine = gameEngine;
GLogic = gameLogic;

m_bg = new sf::Texture();
m_bgs = new sf::Sprite();

//open BG file
if(!m_bg->LoadFromFile("img\\bg\\mainbg.PNG"))
{
GEngine->WriteLog("mainbg.PNG cannot be loaded");
return false;
}

GEngine->guiManager()->NewButton(1,50,50);

return true; //true on succesfull initialization
}


void GS_Menu::Draw()
{
DrawBackground();
}

void GS_Menu::DrawBackground()
{
m_bgs->SetTexture(*m_bg);
m_bgs->SetPosition(0,0);

GEngine->graphicsManager()->App()->Draw(*m_bgs);
}


robvleugel

  • Newbie
  • *
  • Posts: 29
    • View Profile
Unhandled exception on window creation
« Reply #7 on: March 04, 2012, 09:29:17 pm »
Pff so after struggling with breakpoint errors for over 3 weeks, I finaly found out what caused all my problems.

I've been using a game-state system that I found on some tutorial website without spending too much attention to it as it worked fine in the beginning. After changing this gamestate system I have no problems anymore. It seems that the singleton-gamestates I've used somehow caused trouble with the static sfml object members, but not with pointers to them. I don't know why exactly.

So no bug in SFML after all :)