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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - minkcv

Pages: [1]
1
Window / [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?

Pages: [1]