c++segmentation-faultsfmldelete-operatordestruction

Segmentation fault when trying to destroy sf::Font


I am working with the SFML package and receive a segmentation fault when trying to close the window/program. I have located the line in my code that results in segmentation fault, which is when I try to destroy the sf::Font used for drawing text in the SFML window.

The line is simply:

delete button_font; (which is of type sf::Font*)

What I read from the SFML documentation, the sf::Font class does have a destructor. I also tested it right after defining the font object, it destructed itself as expected.

The font is loaded by sf::Font::loadFromFile(~). It is used as argument by reference in some classes used as game states and menus. Could it be a problem that it is used by several sf::Text objects, when I try to delete the font?


Solution

  • Pointers and dynamic memory is one of the tricky things in C++. There is many ways to fail, my guess would be that you deleted a pointer that you did not create with new in the first place or maybe you deleted it twice because two objects held a pointer to it.

    While there may be a good solution for your direct problem, the real solution is using a smart pointer.