When using SDL_Surfaces to handle images, I run into the problem that running SDL_FreeSurface(SDL_Surface *) (doc) twice on the same pointer yields a segmentation fault.
I understand why that happens, but I need to know how I can avoid that. I’d like to check the state of the pointer (find out if its pointing to an existing surface) and then Free the surface only if necessary.
How can I do that?
You write a class that encapsulates an SDL_Surface pointer which frees the surface in it’s destructor. Also, make sure you properly implement or disable the copy constructor and the assignment operator. (see The rule of 3)Then, you never work directly with SDL_Surface pointers again.
Here’s an example of a wrapper around SDL that I was working on a while ago.
I stopped work once I learned about SFML.