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

Author Topic: [Solved] Poll event yields close when program starts  (Read 3262 times)

0 Members and 1 Guest are viewing this topic.

minkcv

  • Newbie
  • *
  • Posts: 2
    • View Profile
[Solved] Poll event yields close when program starts
« on: April 14, 2020, 09:53:43 pm »
I'd been having strange crashes on program start and I have been able to produce a minimal example and counter example. I am using Visual Studio 2017, dynamically linking, and creating a 64 bit executable. I have verified that I am using the correct libraries for debug and for release. The issue only seems to happen in release though. When I include code to recreate the window, the program crashes. Even if the code is not executed. Here is my example

#include <SFML/Graphics.hpp>

int main()
{
   sf::RenderWindow* window = new sf::RenderWindow(sf::VideoMode(200, 200), "SFML works!");
   sf::RenderWindow* w2 = nullptr;
   sf::CircleShape shape(100.f);
   shape.setFillColor(sf::Color::Green);

   while (window->isOpen())
   {
      sf::Event event;
      window->pollEvent(event);
      printf("type: %d\n", event.type);
      if (event.type == sf::Event::Closed)
         window->close();
      if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
         window->close();
     
      if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
      {
         printf("recreate\n");
         window->close();
         delete window;
         window = new sf::RenderWindow(sf::VideoMode(200, 200), "SFML works again");
      }
     
      window->clear();
      window->draw(shape);
      window->display();
   }

   return 0;
}



The "recreate" print doesn't happen even when it crashes, and it prints "type: 0" for window close.
Commenting out the "window = new sf::RenderWindow..." in the loop causes it to run fine. It prints "type: 200" for the event type.
I'm confused how just including the line to "new" the window causes this issue even though it is not executing. Am I making some C++ mistake with the window memory?
« Last Edit: April 15, 2020, 04:07:21 am by minkcv »

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Poll event yields close when program starts
« Reply #1 on: April 15, 2020, 12:28:12 am »
Are you sure the program is crashing and not just exiting by hitting your call to window->close()? The SFML documentation on pollEvent() says

Quote
This function is not blocking: if there's no pending event then it will return false and leave event unmodified

You are treating the event as if it was filled with something even though it might not have been. You might be reading an uninitialized event.type variable leading to undefined behavior. pollEvent() should be used in a while loop like this

sf::Event event;
while (window.pollEvent(event))
{
   // process your event...
}
 


minkcv

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Poll event yields close when program starts
« Reply #2 on: April 15, 2020, 12:59:12 am »
Sorry, I shouldn't have said "crash". Thanks for the info on the while loop. That resolved my issue.

 

anything