Freeing Textures with SDL_DestroyTexture

I’ve been following this book: http://www.packtpub.com/simple-directmedia-layer-game-development/book

At one point, the author implements a TextureManager Class which has a function similar to this for loading and mapping textures (with map() ):

Code:
/* Removed error checking for clarity */

bool TextureManager::load(std::string fileName, std::string id, SDL_Renderer* pRenderer) {
SDL_Surface* tmpSurface = NULL;
SDL_Texture* newTexture = NULL;

/* Load image at specified path into surface */
tmpSurface = IMG_Load(fileName.c_str());

/* Create texture from surface */
newTexture = SDL_CreateTextureFromSurface(pRenderer, tmpSurface);

/* Get rid of tmp surface */
SDL_FreeSurface(tmpSurface);
tmpSurface = NULL;

/* Map the texture to id */
TextureManager::m_textureMap[id] = newTexture;
return true;

}

However, I’m not certain if this would not constitute memory leakage, as SDL_DestroyTexture() is never called.
All that is called in the ‘cleanup phase’ is something like m_textureMap.clear(); (map.clear) which as far as I understand would only delete the mapped reference to the loaded textures.

Am I right, or does the map function copy data? And does, once the load function returns and the scope is lost, ‘newTexture’ get popped off the stack making the call to DestroyTexture() redundant?
Also, DestroyRenderer() gets called in the ‘cleanup phase’, and looking at that code: (SDL_render.c)

Code:
/* Free existing textures for this renderer */
while (renderer->textures) {
SDL_DestroyTexture(renderer->textures);
}

it would also seem that no matter what, my textures will be freed. Or will they?? [Rolling Eyes]

Well if you are not removing textures while the program is running then it doesn’t matter if you call SDL_DestroyTexture() as long as you are not making a ton of textures until memory runs out. Since its dealing with pointers and heap memory, if you want proper memory management by right you must be calling SDL_DestroyTexture() wherever you are removing a texture or when you are clearing the map. The same goes for anything that use the word new, you have to call delete else it will remain there forever.

hungerfish wrote:> I’ve been following this book: http://www.packtpub.com/simple-directmedia-layer-game-development/book

At one point, the author implements a TextureManager Class which has a function similar to this for loading and mapping textures (with map() ):

Code:
/* Removed error checking for clarity */

bool TextureManager::load(std::string fileName, std::string id, SDL_Renderer* pRenderer) {
SDL_Surface* tmpSurface = NULL;
SDL_Texture* newTexture = NULL;

/* Load image at specified path into surface */
tmpSurface = IMG_Load(fileName.c_str());

/* Create texture from surface */
newTexture = SDL_CreateTextureFromSurface(pRenderer, tmpSurface);

/* Get rid of tmp surface */
SDL_FreeSurface(tmpSurface);
tmpSurface = NULL;

/* Map the texture to id */
TextureManager::m_textureMap[id] = newTexture;
return true;

}

However, I’m not certain if this would not constitute memory leakage, as SDL_DestroyTexture() is never called.
All that is called in the ‘cleanup phase’ is something like m_textureMap.clear(); (map.clear) which as far as I understand would only delete the mapped reference to the loaded textures.

Am I right, or does the map function copy data? And does, once the load function returns and the scope is lost, ‘newTexture’ get popped off the stack making the call to DestroyTexture() redundant?
Also, DestroyRenderer() gets called in the ‘cleanup phase’, and looking at that code: (SDL_render.c)

Code:
/* Free existing textures for this renderer */
while (renderer->textures) {
SDL_DestroyTexture(renderer->textures);
}

it would also seem that no matter what, my textures will be freed. Or will they?? [Rolling Eyes]

you have to call delete else it will remain there forever.

Generally speaking you are of course right. I’m just not clear on if it is necessary in this case, boiling down to how SDL is structured. It seems to me that it does do minimalist memory management, or else the code above (taken from the mentioned book) would be ‘wrong’ in the sense that it does not take care of its resources properly.
I find the SDL documentation severely lacking, hence my confusion and my asking. [Question]

mTexture is a pointer, it’s assigned to a TextureManager texture array at
index id, so i assume that is also texturw pointers array. Meaning the
texture pointer is kept alive while load, good common sense. At unload you
mention clear(), or that destroy loop, anywho not a map.clear() in the C++
sense, As the Map Comment does not literally use c++ map, if it’s just an
array of texture pointers, c mapping.
Note if you keep the pointer adress value, it’s valid until you free.
Doesn’t matter if you lost the variable used to create, as soon as you
copied the adrress value (the “map”).
On another news, there is a group thar think you don’t need to free before
exit, leaving that to a “”“sane”"" os, whatever that means and
unportabilizes. ;-D