Search code examples
c++visual-c++sdl-2sdl-ttfsdl-mixer

SDL_TTF and SDL 2 not working together. Giving Unhandled Exception


So I am trying to make a text renderer in SDL2. And when I am updateing the text every frame of the game, eventually it gives me this error:

Unhandled exception at 0x6C7B543D (SDL2.dll) in Games.exe: 0xC0000005: Access violation reading location 0x00000004.

It is strange because it does not give me the exception instantly, only after a while (This time seems to vary, And I think SDL_Mixer is causing it) When I 'Break' The exception I seems that this is the line causing the error (When I create the texture from the surface):

void Text::SetText(SDL_Renderer* rend, std::string message)
    {
        SDL_Color textCol = { Col.Red, Col.Green, Col.Blue, Col.Alpha };

        //Load image at specified path
        SDL_Surface* loadedSurface = TTF_RenderText_Solid(font, message.c_str(), textCol);
        if (loadedSurface == NULL) Debug::Fatal("Could not load text");

        //Create texture from surface pixels
        /*EXCEPTION ON THIS LINE --->*/ SDL_Texture* newTexture = SDL_CreateTextureFromSurface(rend, loadedSurface);

        if (newTexture == NULL) Debug::Fatal("Could not create texture from text");

        Scale.x = loadedSurface->w;
        Scale.y = loadedSurface->h;

        //Get rid of old loaded surface
        SDL_FreeSurface(loadedSurface);

        Texture = newTexture;
    }

I am using the 32 bit visual studio version of SDL_TTF.

Please if you can help fix this bug it would be greatly appreciated. Thank you.

The desired behaviour is that it can load text, without eventually giving an Exception.

Others have had this error, but the solution does not help (As I cannot find a visual studio version on the updated repo): Getting SDL_ttf to play nice with SDL2

EDIT 1:

After updating to a slightly newer version of SDL_TTF 2 The error is still not fixed, any suggestions?

EDIT 2:

After calling:

SDL_GetError()
IMG_GetError()
TTF_GetEror()

This is the output in the console:

CreateTexture(): UNKNOWN
CreateTexture(): UNKNOWN
CreateTexture(): UNKNOWN

It seems that "TTF_RenderText_Solid" is returning NULL. By the way, I am using the newest version of SDL_TTF.


Solution

  • As it's suggested in the post you quoted, you should update your SDL_ttf version for it to work with SDL2.

    Here is the SDL_TTF SDL2 version available for visual studio. http://hg.libsdl.org/SDL_ttf/file/62fc3433538d/VisualC

    You will find the SDL_ttf.sln that you permit you to build the SDL_ttf library ;).