Consider the following C++ function:
SDL_Surface* loadBMP(std::string path, SDL_Surface* loadedBMP){
//Load bitmap
SDL_Surface* loadedBMP = SDL_LoadBMP(path);
if (loadedBMP == NULL){
printf("Unable to load image %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
}
return loadedBMP;
//Magic
SDL_FreeSurface(loadedBMP);
}
Now, for the sake of this question, assume that loadedBMP
is a previously declared global variable.
Here is my question:
Is there a way to let a function continue running after a return
statement? In terms of this function, is there a way to have the final line, SDL_FreeSurface(loadedBMP)
, run after return
ing loadedBMP
?
No. But yes. No line of the function will be executed after the return statement. However, the return statement also marks the end of the function and therefor the end of the scope. So if you manage to have an object on the stack (like a local variable), it's destructor will be called.
But that's not what you want. You don't want to free what you return, not even after the return statement.