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

Author Topic: SFML 2.0 crash after main()  (Read 2126 times)

0 Members and 1 Guest are viewing this topic.

sonicd007

  • Newbie
  • *
  • Posts: 5
    • View Profile
SFML 2.0 crash after main()
« on: January 30, 2013, 07:32:04 pm »
Hey everyone,

I'm using SFML 2.0 and I've built it from source (Windows 7 Visual Studio 2010)

I get the following error after the program exits from main

Code: [Select]
An internal OpenGL call failed in Texture.cpp (95) : GL_INVALID_OPERATION, the s
pecified operation is not allowed in the current state

Any idea if this is a library error or is it something in my source code that could cause this?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: SFML 2.0 crash after main()
« Reply #1 on: January 30, 2013, 07:43:15 pm »
Hi! :)

Can you provide a complete and minimal example that reproduces the problem?
Which type of the libraries did you link (i.e. don't mix release/debug, static/dynamic)?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

sonicd007

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: SFML 2.0 crash after main()
« Reply #2 on: January 30, 2013, 07:59:28 pm »
Sure

linker:
Code: [Select]
sfml-system-d.lib
sfml-main-d.lib
sfml-graphics-d.lib
sfml-window-d.lib
sfgui-d.lib
glew32.lib

Source Files:

main.cpp
Code: [Select]
#include <SFML\System.hpp>
#include <SFML\Window.hpp>
#include <SFML\Graphics.hpp>
#include <SFGUI\SFGUI.hpp>
#include "GameEngine.h"
#include "IntroState.h"

int main(int argc, char* argv[]) {
CGameEngine game;

// initialize the engine
game.Init( "Engine Test v1.0" );

// load the intro
game.ChangeState( CIntroState::Instance() );

// main loop
while ( game.Running() )
{
game.HandleEvents();
game.Update();
game.Draw();
}

// cleanup the engine
game.Cleanup();

return EXIT_SUCCESS;
}

gameengine.h
Code: [Select]
#pragma once

#include <SFML\Graphics.hpp>

#include <vector>
using namespace std;

class CGameState;

class CGameEngine
{
public:

//void Init(const char* title, int width=640, int height=480,
//       int bpp=0, bool fullscreen=false);
void Init(const char* title, int width=800, int height=600);
void Cleanup();

void ChangeState(CGameState* state);
void PushState(CGameState* state);
void PopState();

void HandleEvents();
void Update();
void Draw();

bool Running() { return m_running; }
void Quit() { m_running = false; }

sf::RenderWindow app_window;

private:
// the stack of states
vector<CGameState*> states;

bool m_running;
bool m_fullscreen;
};

gameengine.cpp
Code: [Select]
#include "GameEngine.h"
#include "GameState.h"

void CGameEngine::Init(const char* title, int width, int height)
{
app_window.create( sf::VideoMode( width, height ), title, sf::Style::Titlebar | sf::Style::Close );
printf("CGameInit\n");
}

void CGameEngine::Cleanup()
{
// cleanup the all states
while ( !states.empty() ) {
states.back()->Cleanup();
states.pop_back();
}

printf("CGameEngine Cleanup\n");
}

void CGameEngine::ChangeState(CGameState* state)
{
// cleanup the current state
if ( !states.empty() ) {
states.back()->Cleanup();
states.pop_back();
}

// store and init the new state
states.push_back(state);
states.back()->Init();
}

void CGameEngine::PushState(CGameState* state)
{
// pause current state
if ( !states.empty() ) {
states.back()->Pause();
}

// store and init the new state
states.push_back(state);
states.back()->Init();
}

void CGameEngine::PopState()
{
// cleanup the current state
if ( !states.empty() ) {
states.back()->Cleanup();
states.pop_back();
}

// resume previous state
if ( !states.empty() ) {
states.back()->Resume();
}
}

void CGameEngine::HandleEvents()
{
// let the state handle events
states.back()->HandleEvents(this);
}

void CGameEngine::Update()
{
// let the state update the game
states.back()->Update(this);
}

void CGameEngine::Draw()
{
// let the state draw the screen
states.back()->Draw(this);
}

gamestate.h
Code: [Select]
#pragma once

#include "GameEngine.h"

class CGameState
{
public:
virtual void Init() = 0;
virtual void Cleanup() = 0;

virtual void Pause() = 0;
virtual void Resume() = 0;

virtual void HandleEvents(CGameEngine* game) = 0;
virtual void Update(CGameEngine* game) = 0;
virtual void Draw(CGameEngine* game) = 0;

void ChangeState(CGameEngine* game, CGameState* state) {
game->ChangeState(state);
}

protected:
CGameState() { }
};

introstate.h
Code: [Select]
#pragma once

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

class CIntroState : public CGameState
{
public:
void Init();
void Cleanup();

void Pause();
void Resume();

void HandleEvents(CGameEngine* game);
void Update(CGameEngine* game);
void Draw(CGameEngine* game);

static CIntroState* Instance() {
return &m_IntroState;
}

protected:
CIntroState() { }

private:
static CIntroState m_IntroState;

sf::Sprite bg;
sf::Texture* bgTexture;
int alpha;
};

introstate.cpp
Code: [Select]
#include "IntroState.h"
#include "TextureManager.h"

CIntroState CIntroState::m_IntroState;

void CIntroState::Init()
{
bgTexture = gTextureManager.getResource("C:\\Users\\SonicD007\\Desktop\\DragonBall Zeta\\BMPs\\loginscreen.png");
if (bgTexture != NULL)
bg.setTexture(*bgTexture);
bg.setPosition(0,0);
}


void CIntroState::Cleanup()
{
//if (bgTexture != NULL)
//gTextureManager.releaseResource("C:\\Users\\SonicD007\\Desktop\\DragonBall Zeta\\BMPs\\loginscreen.png");
}

void CIntroState::Pause()
{

}

void CIntroState::Resume()
{

}

void CIntroState::HandleEvents(CGameEngine* game)
{
sf::Event event;
while (game->app_window.pollEvent(event) )
{
switch (event.type)
{
case sf::Event::Closed:
game->Quit();
break;
}
}

}

void CIntroState::Update(CGameEngine* game)
{

}

void CIntroState::Draw(CGameEngine* game)
{
game->app_window.clear();
game->app_window.draw(bg);
game->app_window.display();
}

resourcemanager.h
Code: [Select]
#pragma once

#include <string>
#include <map>

template< class T >
class ResourceManager {
public:
typedef std::pair< std::string, T* > Resource;
typedef std::map< std::string, T* > ResourceMap;

private:
ResourceMap m_resource;

T* find( const std::string& strId ) {
T* resource = NULL;
typename ResourceMap::iterator it = m_resource.find( strId );
if ( it != m_resource.end() ) {
resource = it->second;
}

return resource;
}

protected:
virtual T* load( const std::string& strId ) = 0;

public:
ResourceManager(){}

virtual ~ResourceManager() { releaseAllResources(); }

T* getResource( const std::string& strId) {
T* resource = find( strId );
if ( resource == NULL ) {
resource = load( strId);
// If the resource loaded successfully
if ( resource != NULL )
m_resource.insert( Resource( strId, resource ) );
}
return resource;
}

void releaseResource( const std::string& strId) {
T* resource = find( strId );
if ( resource != NULL ) {
delete resource;
m_resource.erase( m_resource.find( strId ) );
}
}

void releaseAllResources() {
while( m_resource.begin() != m_resource.end() )
delete m_resource.begin()->second;
m_resource.erase( m_resource.begin() );
}
};

texturemanager.h
Code: [Select]
#pragma once

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

class TextureManager : public ResourceManager< sf::Texture > {
private:
protected:
virtual sf::Texture* load( const std::string& strId );
public:
};

extern TextureManager gTextureManager;

texturemanager.cpp
Code: [Select]

#include <iostream>
#include "TextureManager.h"

using namespace std;

TextureManager gTextureManager;

sf::Texture* TextureManager::load( const std::string& strId ) {
sf::Texture* image = new sf::Texture();
if( !image->loadFromFile(strId) ) {
cout << "[WARN] ImageManager failed to load: " << strId << endl;
delete image;
image = NULL;
}

return image;
}

I noticed that when I uncomment the two lines in introstate.cpp cleanup(), that's when the error pops up.  With those lines in there, the program crashes after main but I don't get the opengl error outputted

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: SFML 2.0 crash after main()
« Reply #3 on: January 30, 2013, 08:09:59 pm »
Sorry, that's nowhere near a minimal example, I won't go through all that code. ;)
You also should use the code=cpp tag instead of the plain code tag.

As you've noticed on your own, it's probably caused from your code and not SFML and you should investigate further, narrow the problem down and create a minimal and complete example.
« Last Edit: January 30, 2013, 08:12:56 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

sonicd007

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: SFML 2.0 crash after main()
« Reply #4 on: January 31, 2013, 04:04:26 pm »
You were right, it wasn't SFML causing the problem.  The sample game engine I was using had a bug in it and the sample resource management I was using had the same type of bug -.-".

Basically they were trying to call a vector erase on nothing (in the game engine) and in the resource mangement, they were trying to call a map erase on nothing.  They never checked to see if the size was > 0 which was causing the program to flip out after main.  The destructor of the resource class was getting called after main.

Thanks exploiter :)

(p.s. the code wasn't really long, it just looked that way, most of the methods were empty lol  thank you though ;))

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: SFML 2.0 crash after main()
« Reply #5 on: January 31, 2013, 05:32:12 pm »
p.s. the code wasn't really long
The length is only one part that accounts to minimal, then again 300+ lines of code isn't minimal and it doesn't matter if there were just empty function bodies or not. See here. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/