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

Author Topic: Crash with RenderWindow and Texture as class member variable  (Read 1004 times)

0 Members and 1 Guest are viewing this topic.

olmo14

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Crash with RenderWindow and Texture as class member variable
« on: October 30, 2018, 01:03:12 am »
Hello everyone,

I've been looking for something similar to this error in the internet as well as in this forums and I haven't been able to found anything that solves my problem.

I'm going to simple down the error. I have a main() function where the first thing I create is a sf::RenderWindow. I have an fps counter (still in main()) which is a sf::Text and it displays correctly. However if I create a class (a button, for example) that has a sf::Texture as a member variable and I create a variable on Button, the program crashes in the declaration of sf::RenderWindow. Here is the simplest code I can get to reproduce the error:

// In one file
#ifndef __BUTTON_H__
#define __BUTTON_H__

#include <SFML/Graphics/RenderWindow.hpp>
#incldue <SFML/Graphics/Text.hpp>

class Button {
public:
  Button();
  ~Button();

  void draw(sf::RenderWindow* window);

private:
  sf::Text m_text;
};

#endif // __BUTTON_H__

// ============================================================================
// In other file
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Text.hpp>
#include <SFML/Window/Keyboard.hpp>
#include <SFML/Window/Event.hpp>

int main() {
  sf::RenderWindow window(sf::VideoMode(800, 600), "");
 
  sf::Font font;
  font.loadFromFile("resources/fonts/arial.ttf");
  sf::Text text;
  text.setFont(font);
  text.setCharacterSize(40);
  text.setPosition(400, 300);
  text.setString("0");

  while (window.isOpen()) {
    // Input
    sf::Event e;
    if (window.pollEvent(e)) {
      if (e.type == sf::Event::Closed) {
        window.close();
        break;
      }
    }

    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) {
      window.close();
      break;
    }    

    // Draw
    window.clear();

    window.draw(text);

    window.display();
  }

  return 0;
}
 


That code would run correctly. But in the moment I include button.h lets say, and create a Button object the program crashes as I mentioned. Just ignore that the counter always displays 0 and that I don't make any check for if loading the font correctly (it loads correctly).

I tried to debug and found that the program crashes in toAnsiString() when parsing the window's title, I think. Oddly enough, if I declare a std::string just above the declaration of the RenderWindow the program runs, but the graphics displayed are just garbage.
int main() {
  std::string s("anything");
  sf::RenderWindow window(sf::VideoMode(800, 600), "");
}
 

Any ideas? Thanks in advance.




Kind regards,


Olmo.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Crash with RenderWindow and Texture as class member variable
« Reply #1 on: October 30, 2018, 07:49:29 am »
Sounds like you're mixing different, incompatible runtime libraries.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

olmo14

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Crash with RenderWindow and Texture as class member variable
« Reply #2 on: October 30, 2018, 12:49:49 pm »
Thank you very much for your response. It gave me some direction to go to. The thing is that I started the project in Windows (however it's multiplatform) and so downloaded the link for Visual Studio 17. I made a shared project structure for when adding new platform (such as Linux in the moment I'm writing this) where I had an include folder with the headers and a lib folder with the libraries. When I downloaded the Linux .zip from the web, I only copied the Linux libraries to the lib folder. The project worked as I commented above but gave me the problem I said. Now it results that if I replace the headers provided in the Windows .zip with the Linux .zip, the project works. The thing is that both .zip are for the same SFML version, the 2.5.1. Any insights on this?

Everything works now, I guess I'll have to create two folders for the headers though.




Kind regards,


Olmo.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Crash with RenderWindow and Texture as class member variable
« Reply #3 on: October 30, 2018, 03:16:46 pm »
If you use the SFML version installed through the package manager, you should also use those headers.
But in general there's just one version of the headers and they should work fine across platforms.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything