2

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?

Smartskaft2
  • 468
  • 3
  • 16
  • Do you create the `button_font` using `new`? If not, you should not `delete`it either. – Bo Persson Nov 28 '17 at 11:20
  • No, I did not think of that. But it is stored as a pointer in a class variable. I though all pointers are to be destroyed properly by explicitly defined destructors? (I still have not grasped the proper use of constructors and destructors fully) – Smartskaft2 Nov 28 '17 at 12:20
  • 1
    You can have pointers to non-dynamic objects too. Like `int i; int* p = &i;` where `p` is a pointer to `i`, but should not be `delete`'d. – Bo Persson Nov 28 '17 at 12:36
  • I removed all unnecessary `delete` calls and let the end of scope handle the destruction of pointers not declared by `new`. it solved my initial problem. However, I am still using the wrong approach since (as I expected) my code leaks a lot of memory (checked with Valgrind). I guess I should clear the "pointed memory" without ´delete´, but I am not sure in what way. Maybe with ´clear()´? Thank you for helping out, despite the amateur mistakes. – Smartskaft2 Nov 28 '17 at 13:14
  • Can you share the code example? – Martin Sand Dec 01 '17 at 07:49
  • I found the problem, it was indeed that I was trying to `delete` a variable not created with `new`. – Smartskaft2 Dec 01 '17 at 11:35
  • I can also add that sometimes I checked memory leaks with Valgrind over a remote desktop application (ThinLinc). When connecting to the schools at the university via this application, I had some minor memory leaks which I did not have when I ran the same code from the actual computers. The leak came from the application, not my code. I have no idea why this happens, but in case someone else stumbles upon this it might be nice to know. – Smartskaft2 Feb 13 '18 at 08:20

1 Answers1

1

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.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • I did consider it. However, I want to learn how to use pointers the right way. I have used smart pointers before, and realize it's benefits. Therefore, for this project (I'm quite new to c++, I'm taking a university course on it right now) I want to use regular pointers. My new strategy is to create all pointers with `new` and free the memory with `delete` Being consistent, I should be able to avoid memory leaks and other problems. – Smartskaft2 Dec 01 '17 at 11:34
  • its* If anyone wonders, we did succeed using regular pointers. But as already stated, if it was a bigger project it would be more difficult to make sure all memory is cleared properly. I will probably never use that method again, but I learned a lot from it so I am glad I tried it. – Smartskaft2 Feb 13 '18 at 08:05