SFML community forums

Help => General => Topic started by: sircuddles on February 21, 2013, 02:55:29 am

Title: Random crashes?
Post by: sircuddles on February 21, 2013, 02:55:29 am
I've had this problem and thought it was probably just on my end so I've just sort of been dealing with it.  It seems that randomly, when moving the mouse around, my window simply closes.  This has happened across multiple projects and now with multiple computers, and I'm not sure where the issue lies. 

I recently started coding with a friend (using Git) and he also has the same problem.  We're both using VS12 and got the precompiled packages from exploit's nightly builds.  If you don't move the mouse, there is no problem.  When you do move the mouse, within a few seconds (usually) the window closes on it's own.

I can't seem to find any answers on Google about this particular issue.
Title: Re: Random crashes?
Post by: zsbzsb on February 21, 2013, 03:34:51 am
I've had this problem and thought it was probably just on my end so I've just sort of been dealing with it.  It seems that randomly, when moving the mouse around, my window simply closes.  This has happened across multiple projects and now with multiple computers, and I'm not sure where the issue lies. 

I recently started coding with a friend (using Git) and he also has the same problem.  We're both using VS12 and got the precompiled packages from exploit's nightly builds.  If you don't move the mouse, there is no problem.  When you do move the mouse, within a few seconds (usually) the window closes on it's own.

I can't seem to find any answers on Google about this particular issue.
Please read this (http://en.sfml-dev.org/forums/index.php?topic=5559.0). Unless you post your code we have no idea what you are doing.

This issue sounds like another issue someone posted and the problem was that they were incorrectly handling events... But without your code we can't help you.
Title: Re: Random crashes?
Post by: FRex on February 21, 2013, 03:35:05 am
Show the minimal code that contains the problem, maybe you are doing
if(event.key.code==sf::Keyboard::Escape)window.close();
or similar?
Title: Re: Random crashes?
Post by: sircuddles on February 21, 2013, 04:00:09 am
I didn't post any code because this has happened over many projects since I started using SFML, as I said in the original post.  There isn't any line of code that I can post that I suspect to be the problem, because the problem happens all the time. 

This is the code from the current project main loop (the one that crashes for both me and my partner).  The problem occurs with and without the custom mouse cursor code:

        while (gameWindow.isOpen())
        {
                while (gameWindow.pollEvent(gameEvents))
                {
                        if ((gameEvents.key.code == sf::Keyboard::Escape) || (gameEvents.type == sf::Event::Closed))
                                gameWindow.close();
                       
                }

                thing.update(gameTime);
                // Horrible update that sets the cursor to the mouse position
                customCursor.update(sf::Vector2f(sf::Mouse::getPosition(gameWindow)));
               
                Board::GetInstance(gameWindow)->draw();
                thing.draw();
                customCursor.draw();

                gameWindow.display();
                gameWindow.clear(sf::Color::White);

                gameTime = gameClock.restart().asSeconds();
    }
Title: Re: Random crashes?
Post by: FRex on February 21, 2013, 04:16:44 am
  if ((gameEvents.type==sf::Event::KeyPressed&&gameEvents.key.code == sf::Keyboard::Escape) || (gameEvents.type == sf::Event::Closed))
is correct.
You can't access any field in event except type without knowing the type first or it's ub.
There's official tutorial about that: http://www.sfml-dev.org/tutorials/2.0/window-events.php
Title: Re: Random crashes?
Post by: sircuddles on February 21, 2013, 04:26:45 am
Initial testing shows that worked, thanks.

So when you try to access something like event.key.code when there is no KeyPressed event, event.key.code is undefined and accessing it results in random garbage? 
Title: Re: Random crashes?
Post by: FRex on February 21, 2013, 04:38:08 am
Yes, but enums are often* same size as int so when mouse x(int) was same value as sf::Keyboard::Escape value of sf::Keyboard::Key enum(here with underlying type int) then the window closed because bits aligned just right to make it look like escape code.

*often = not guaranteed by standard, not reliable, not portable, dependand on compiler, os and settings ect.
Title: AW: Re: Random crashes?
Post by: eXpl0it3r on February 21, 2013, 06:51:00 am
*often = not guaranteed by standard, not reliable, not portable, dependand on compiler, os and settings ect.
= undefined = not even worth thinking about it = not even worth mentioning ;)