Search code examples
c++segmentation-faultsfml

Segmentation fault in SFML when i try to draw an sf::Text object


I'm using SFML for a school project, and I'm having this problem when I try to run this code I get the error: double free or corruption (out) and the program crashes. My operating system is ubuntu.

I tried creating "text" with a malloc. In that case, there aren't any errors but it crashes anyway (I still get the segmentation fault). I even tried sending this code to a friend, it works for him so I think there is something wrong with my configuration or something?

int main(){
    sf::RenderWindow window(sf::VideoMode(500, 320), " Text ");
    sf::Event event;
    sf::Font font;
    font.loadFromFile("../arial_narrow_7.ttf");
    sf::Text text("hello", font);
    text.setCharacterSize(30);
    text.setStyle(sf::Text::Bold);
    text.setFillColor(sf::Color::Red);
    text.setFont(font);

    while(window.isOpen()) {
        window.draw(text);
        window.display();
        window.clear();
    }
}

It should draw the text "hello" in red color, but as I said the program crashes.


Solution

  • OK, as Bernard suggested, the problem was a external to the code itself, my version of SFML was too old, 2.3 i think, i didn't notice it because i tried to update it with the command sudo upgrade/ sudo update and it said that everything was up to date, so when i noticed the version in the SFML/Config.hpp file was older, I reinstalled SFML manually whit the newest files form the SFML website. Thanks everyone for your time and your helpful tips :)