-2

I'm currently testing a TextureManager class I'll use in my game project but the problem i have is that the .exe crashes when i'm trying to load 2d textures to a std::map. What i need is that I have a map of pointers to SDL_Textures, keyed with a standard string

std::map<std::string, SDL_Texture*>Textures; 

SDL and SDL_image initializes ok (success printfs in console) but then Texture mapping starts and the program crashes, as soon as load() function is called. Below the implementation of the function

void TextureManager::load( std::string path, std::string id )
{
    SDL_Surface* TempSurface = IMG_Load( path.c_str() );
    if( TempSurface == NULL )
    {
        printf( "Failed to load %s: error: %s\n", path.c_str(), IMG_GetError() );
    }

    /* add color coding if necessary here
    SDL_SetColorKey( TempSurface, SDL_TRUE, SDL_MapRGB( TempSurface->format, 0, 0xFF, 0xFF ) );
    */

    SDL_Texture* newTexture = SDL_CreateTextureFromSurface( renderer, TempSurface );
    if( newTexture == NULL )
    {
        printf( "Failed to create a texture %s: error: %s\n", id.c_str(), SDL_GetError() );
    }

    SDL_FreeSurface( TempSurface );

    Textures[ id ] = newTexture;

    printf( "Texture %s mapped successfully", id.c_str() );

    //possible error - not the cause of the described error
    //SDL_DestroyTexture( newTexture );

}

Naturally, Textures map is a private member of the TextureManager class. It doesn't matter if the function is called by the TextureManager's constructor, or later by the pointer to the TextureManager - same result. The previous version of this loader was not mapping textures, map was not used at all, and it worked just fine. Is the map then to blame? It's the only difference between the versions...

edit: i have added additional comment to the last code line and 'commented' it out, as after I tested the suggestions it is the reason for error, the behavior of program didn't change so it has to be smth else.

EDIT2: OK, I have resolved the issue - it happened that one of the surfaces was loaded from a .jpg file and for some reason the jpg sdl library file was corrupted. JPG support was not, therefore, initialized during runtime and an attempt to load a jpg file was crashing the exe. Replacing the .dll in the project folder have solved the issue - everything is working as expected now

Quit
  • 22
  • 6

1 Answers1

0
Textures[id] = newTexture;
SDL_DestroyTexture(newTexture);

This leaves a pointer to a texture which has been freed inside the map<std::string, SDL_Texture*> (or similar data structure you are using). You should destroy the texture only when you don't need anymore, not just after loading it.

It's a dangling pointer because SDL_CreateTextureFromSurface returns an address to a valid SDL_Texture instance which becomes invalid after destroying it, so Textures[id] points to an address which is no more allocated.

Jack
  • 131,802
  • 30
  • 241
  • 343
  • I was suspecting as much that this can be a cause of a future trouble so I tagged it with a comment for reference. However, this is not the cause of the issue. I built the exe without this line and the behavior didn't change a bit. The load() function seems to be crashing the program immediately - i have added printf's after successful IMG load and SDL createTexture, none of these appear in the console – Quit Jun 06 '15 at 21:29