You're using the refcount incorrectly. SDL_FreeSurface
will decrement the refcount, and when called on a null pointer, is a no-op. So, your assignment operator should look like this:
const Image& Image::operator=(const Image& other){
if (img != other.img){
SDL_FreeSurface(img);
img = other.img;
if (img != NULL)
img->refcount++;
}
return *this;
}
And your destructor should look like this:
Image::~Image(){
SDL_FreeSurface(img);
}
Also, in your default constructor, you should initialize img to a null pointer.
Image::Image() :img(NULL) {}
See the second to last example on this page: http://sdl.beuc.net/sdl.wiki/SDL_Surface