(Note: I’m coding in Pascal)
In the creator of a class TQuest, I initialize some objects with a pSDL_Surface given as a parameter as follows :
quests.Add( CPopuQuest.create('Get money ! ','Get more than $10,000', IMG_LOAD(MONEY_ICON), 1000, 10) );
(The interesting part is IMG_LOAD(MONEY_ICON) with MONEY_ICON the path to an image.
When I end my program, I get an error at the destructor of this CPopuQuest object in this line:
if badge<>nil then SDL_FreeSurface(badge)
(badge is the attribute where the pSDL_Surface instantiated by IMG_LOAD(MONEY_ICON) is copied)
It works most of the time, but from time a time I still get an error when closing the program … If anyone can help 🙂 (as you can see I’m quite confused with how IMG_LOAD works, what it returns, and how memory is allocated when its called).
Anything that
IMG_Loadreturns will be safe to pass toSDL_FreeSurface, even if it returns a null pointer. (Thus, you don’t need to checkbadge <> nilbefore callingSDL_FreeSurface(badge).)If your program crashes when you call
SDL_FreeSurface, then it’s due to other problems in your program. That function is the correct way to free anything thatIMG_Loadloads. Everything shown in this question is right.