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

Author Topic: Crash on pollEvent  (Read 6082 times)

0 Members and 1 Guest are viewing this topic.

Oilfan

  • Newbie
  • *
  • Posts: 12
    • View Profile
Crash on pollEvent
« on: June 09, 2013, 06:17:16 pm »
Hello. I have a problem in my code but i dont know how to fix.
Here is the problematic code:
while(window.isOpen())
{
                sf::Event event;
                while(window.pollEvent(event))
                {
                        stateManager.HandleEvent(event);
                        if (event.type == sf::Event::Closed)
                        {
                                window.close();
                        }
                }
}

the crash happens in window.pollEvent when i start the program.
Here is code that create window:
Game::Game() : window(sf::VideoMode(800, 600), "sfml-app", 7U, sf::ContextSettings(0U, 0U, 10U)), postShader(NULL)
{
        defaultFont.loadFromFile("AllOverAgain.ttf");
}

definition in header:
sf::RenderWindow window;

WHat can i do to fix this?
I can give more code if needed.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Crash on pollEvent
« Reply #1 on: June 09, 2013, 06:23:51 pm »
Can you reduce your code to a minimal and complete example? That is, just a main() function with as few code as possible, but still reproducing the crash. No classes or custom functionality that is not related to the problem.

Are you sure you link everything correctly and don't mix configurations?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Re: Crash on pollEvent
« Reply #2 on: June 09, 2013, 06:26:35 pm »
Damnit Nexus you are fast, was just about to write the same thing :P

Anyway I am curious as of why you are using a unsigned int and not the enums SFML provides for style?
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Oilfan

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Crash on pollEvent
« Reply #3 on: June 09, 2013, 06:34:04 pm »
Ok here is main function:
int main()
{
        if (!sf::Shader::isAvailable())
        {
                return -1;
        }

        try
        {
                Game game;
                game.Run();
        }
        catch (std::exception& e)
        {
                std::cout << e.what() << std::endl;
                std::cin.get();
        }      
}

Here is constructor for Game class, thats where the window is created:
Game::Game() : window(sf::VideoMode(800, 600), "sfml-app", 7U, sf::ContextSettings(0U, 0U, 10U)), postShader(NULL) //Here window is created, i tried other methods but still crashing
{
        defaultFont.loadFromFile("AllOverAgain.ttf");
        frameTime = 0;
}

Here is the Game::Run function (thats whre the crash happens):
void Game::Run()
{
        while(window.isOpen())
        {
                timer.restart();

                sf::Event event;
                while(window.pollEvent(event)) //here crash
                {
                        stateManager.HandleEvent(event);
                        if (event.type == sf::Event::Closed)
                        {
                                window.close();
                        }
                }

                /*
                // Update
                stateManager.Update(frameTime);

                window.clear();
                stateManager.Draw();
                window.display();
                */


                frameTime = timer.getElapsedTime().asSeconds();
                if (frameTime > 0.1)
                {
                        frameTime = 0.1;
                }
        }
}

Anyway I am curious as of why you are using a unsigned int and not the enums SFML provides for style?

They caused some errors about too many arguments or smth
« Last Edit: June 09, 2013, 06:36:52 pm by Oilfan »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Crash on pollEvent
« Reply #4 on: June 09, 2013, 06:48:18 pm »
Please read exactly:
Can you reduce your code to a minimal and complete example? That is, just a main() function with as few code as possible, but still reproducing the crash. No classes or custom functionality that is not related to the problem.

We don't know about other member of your class and hidden functionality. You make it much easier for us to help, if you put all the code into a single main() function. We don't care about the state manager, the shader or clocks -- only the minimal code that causes the problem.

Also, as stated by Groogy, you should use the SFML flags for the style. If you are not sure how to pass them, refer to the docs or tutorials...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Oilfan

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Crash on pollEvent
« Reply #5 on: June 09, 2013, 06:57:10 pm »
Here it is, everything in one main function. And what a surprise, it works now in this program but not in the original.

#include <sfml/Graphics.hpp>
#include <iostream>

int main()
{
        sf::RenderWindow window(sf::VideoMode(800, 600), "sfml-app", 7U, sf::ContextSettings(0U, 0U, 10U)), postShader(NULL);

        while(window.isOpen())
        {
                sf::Event event;
                while(window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                        {
                                window.close();
                        }
                }
        }
}

Console errors:
Failed to set pixel format for device context -- cannot create OpenGL context
Failed to activate the window's context
Failed to activate the window's context

but no crash..

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Crash on pollEvent
« Reply #6 on: June 09, 2013, 07:08:00 pm »
Why are you creating two window instances, and initializing the second with an invalid handle?

Or what do you expect the comma to do there?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Oilfan

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Crash on pollEvent
« Reply #7 on: June 09, 2013, 07:13:20 pm »
Removed it, no console errors now. BUt how is this going to fix the crash if i only keep looking at that minimalist main function which is working?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Crash on pollEvent
« Reply #8 on: June 09, 2013, 07:19:13 pm »
BUt how is this going to fix the crash if i only keep looking at that minimalist main function which is working?
The idea is to look at the minimalist main() function which is not working ;)

Read this post, it explains everything.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Oilfan

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Crash on pollEvent
« Reply #9 on: June 09, 2013, 07:24:27 pm »
The only thing that isnt working is the game::run function.
Unhandled exception at 0x771615de in sfml-app.exe: 0xC0000005: Access violation reading location 0xbaadf00d.
this is what happpens during debug at crash time.
I have already provided code for game::run and it is actually the same code as in that minimalist main function which is working. strange...

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Crash on pollEvent
« Reply #10 on: June 09, 2013, 07:27:54 pm »
Try to reduce your code step by step, until you only have a main() function. After each step, you verify if the error persists.

By the way, see here to know the meaning of the magic addresses in Visual Studio.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Oilfan

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Crash on pollEvent
« Reply #11 on: June 09, 2013, 08:02:57 pm »
SO i made this code which is similiar to the original code but without any other things but the window creation.
This code works, i have no idea why the original doesnt.
#include <sfml/Graphics.hpp>
#include <iostream>

class Game
{
public:
        Game();
        void run();

private:
        sf::RenderWindow window;
};

Game::Game() : window(sf::VideoMode(800, 600), "sfml-app", 7U, sf::ContextSettings(0U, 0U, 10U))
{
}

void Game::run()
{
        while(window.isOpen())
        {
                sf::Event event;
                while(window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                        {
                                window.close();
                        }
                }
        }
}

int main()
{
        Game game;
        game.run();

        return 0;
}

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Re: Crash on pollEvent
« Reply #12 on: June 09, 2013, 08:04:59 pm »
I'm guessing you have a problem in the state manager since that one is removed now and it works.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Oilfan

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Crash on pollEvent
« Reply #13 on: June 09, 2013, 08:08:19 pm »
Thats what i thought too. But i commented out every other thing in the original code but not the window creation and main loop, it doesnt crash during run time now but as soon as i click close on the title bar it crashes.

EDIT: Now it seems to crash in the game deconstructor. In original code.


EDIT2: All crashes were caused by state manager. I deleted it completely and the code works flawlessly!
Thx for help ;)
« Last Edit: June 09, 2013, 08:50:08 pm by Oilfan »