Code: Select all
std::vector<SDL_Surface*> mySurfaces;
SDL_Surface tempSurface = NULL;
tempSurface = SDL_LoadBMP("yourimage.bmp");
tempSurface->refcount += 2; // We increase it to 2, because we don't just want to free the copy, but the temp as well. If we only increase it once here, sure, it will work but we won't be able to free tempSurfe
mySurfaces.push_back(tempSurface);
SDL_FreeSurface(tempSurface); //Sense the refcount was at 2, we have enough to free both the cop(ies) and the original!
SDL_FreeSurface(mySurfaces[0]); // Success!
Whoa lets hold up for a second! From the looks of it you are creating the Surface, then you have decided to free it, the you are freeing it again. The second time you Free the surface you are essentially freeing nothing! you can't free what does not exist. When you have a vector of pointers the address of the object is stored as a pointer NOT the object itself.
As stated by the people above, either do this: SDL_FreeSurface(mySurfaces[0]); or SDL_FreeSurface(tempSurface). Trying to free something twice is not good design, nor should it work, and if it does, its not guaranteed safe, like at all.
This refcount thing states that its for duplicating (and i wouldn't really do that either...).
Code: Select all
SDL_Surface tempSurface = NULL;
tempSurface = SDL_LoadBMP("yourimage.bmp");
tempSurface->refcount += 2; // We increase it to 2, because we don't just want to free the copy, but the temp as well. If we only increase it once here, sure, it will work but we won't be able to free tempSurface
mySurfaces.push_back(tempSurface);
That my friend is not duplicating! It is NOT a copy (in a sense), its a pointer to the address of tempSurface, or should be) since "SDL_Surface tempSurface = NULL;", last i checked that should be a pointer.
What push_back will do is to Copy the pointer, but not the object that the pointer refers to.
Anyone correct me if im wrong, or if i said non understandable crap. But im pretty sure that trying to delete an already deleted object is against c++ and against logic.