SDL_ttf runtime error

This topic is 1646 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all,

Looking for a little help. I have used SDL with TTF quite a bit and I am fairly comfortable with it at this point. This error has me dumbfounded however, and I have no more ideas of how to proceed. I have tested the Font loading and TTF_RenderText_Solid. The surface returned from the rendertext function is not a null pointer, but it must be the issue, as my program crashes anytime I try to blit a text surface. I have used the same blitting function for standard images, and it works just fine.

I have also checked all my compiler linking (I am using code::blocks), it is fine. I have loaded up old programs, recompiled them, and they work just fine. I have no idea where to go from here. My button class is where I am trying to utilize text, so that is what I will be showing.

Edit: In case anyone ends up asking, I did call TTF_Init();


#include "text.h"

std::map <std::string, SDL_Color> Text::mColors;

std::map <std::string, TTF_Font *> Text::mFonts;

SDL_Surface * Text::applyText( std::string strText, std::string strFont, std::string strColor, int * w, int * h )

{

SDL_Surface * surface = NULL;

surface = TTF_RenderText_Solid( mFonts[ strFont ], strText.c_str(), mColors[ strColor ] );

TTF_SizeText( mFonts[ strFont ], strText.c_str(), w, h );

return surface;

}

#ifndef TEXT_H

#define TEXT_H

#include <map>

#include <string>

#include "SDL/sdl_ttf.h"

class Text

{

public:

static SDL_Surface * applyText( std::string strText, std::string strFont, std::string strColor, int * w, int * h );

static std::map <std::string, SDL_Color> mColors;

static std::map <std::string, TTF_Font *> mFonts;

};

#endif // TEXT_H

// inside of Display.cpp

void Display::loadFonts()

{

initInput( "Resources/ResourceListFonts.txt" );

while( !isFile.eof() )

{

readLine();

if( !strData.empty() )

{

std::string strFont = strData;

readLine();

std::string strFontPath = strPath + strData;

int fontSize = readValue();

TTF_Font * newFont = NULL;

newFont = TTF_OpenFont( strFontPath.c_str(), fontSize );

mFonts.insert( std::pair <std::string, TTF_Font *> ( strFont, newFont ) );

}

}

freeInput();

}

// in button.cpp

Button::Button( int iX, int iY, std::string strSurf, std::string strFunc, std::string strText )

{

x = iX;

y = iY;

w = Display::getImageWidth( strSurf );

h = Display::getImageHeight( strSurf );

iTextW = 0;

iTextH = 0;

strFunction = strFunc;

strSurface = strSurf;

surfaceText = NULL;

if( !strText.empty() )

{

strSurfaceText = strText;

surfaceText = Display::applyText( strSurfaceText, "monospaceTypewriter12", "black", &iTextW, &iTextH );

}

sClipX = 0;

sClipY = 0;

}

void Button::render()

{

Display::applySurface( x, y, strSurface, sClipX, sClipY );

if( surfaceText != NULL )

{

Display::applySurface( ( x + ( iTextW / 2 ) ), ( y + ( iTextH / 2 ) ), surfaceText );

}

}


Edited by Wraithe

Share this post

Share on other sites
Are you sure TTF_OpenFont does not return a null pointer?

Are you sure mFonts[strFont] does not return a null pointer?

Share this post

Share on other sites

You need to have error checking around all calls. Almost all SDL calls can return status codes. The convention in SDL is that functions which return pointers can return nullptr on failure, and functions which return integer values return negative values on failure. You can use SDL_GetError() (or for TTF functions,  TTF_GetError()) to get a reasonably human readable description of the error.

Share this post

Share on other sites

Thanks for the replies! As far as I can tell (with error checking) the pointers are valid. I set up error checking and not a single error occurs. The program just crashes when a text rendered surface hits the blit function.

Edit:

Are you sure mFonts[strFont] does not return a null pointer?

Yes, I have even tried filling in all the render text solid parameters with solid values, generated IN that function, and the crash still occured at the blit function.

Edited by Wraithe

Share this post

Share on other sites

I have also tried freeing the rendered text surface immediately after creation, and the program crashes, yet the pointer is not NULL. Again, my other programs work just fine with TTF, what on earth could the issue be?

Edit: Finally got a crash report (Only does it once out of every 10 runs )

Problem signature:
Problem Event Name:    APPCRASH
Application Name:    Space Invaders.exe
Application Version:    0.0.0.0
Application Timestamp:    51e53cc5
Fault Module Name:    ntdll.dll
Fault Module Version:    6.1.7600.16385
Fault Module Timestamp:    4a5bdb3b
Exception Code:    c0000005
Exception Offset:    000335f2
OS Version:    6.1.7600.2.0.0.256.1
Locale ID:    1033
Additional Information 1:    e3cc
Additional Information 2:    e3cc0ee538989a7b66ab90686b3c00a6
Additional Information 3:    d22a
Additional Information 4:    d22a3993b2d48ddd96705e026b50390c

Fault Module Name:    ntdll.dll  <-- What is this?

Edited by Wraithe

Share this post

Share on other sites

Sorry to post so many times in a row, but I found a solution.

The program only crashes when I use a vector of my button class. If I make it a vector of pointers, it works. Anyone know why this is?  (Not a big deal, pointers are fine, I simply don't understand why that would cause a crash).

Share this post

Share on other sites
In certain situations the vector will copy objects stored in the vector so if you have not implemented the Button class to handle copying correctly that is probably the problem.

Also see the rule of three.

Share this post

Share on other sites

Were you not running your program in a debugger? It would be a lot easier to diagnose the cause of the crash using a debugger.

Share this post

Share on other sites

Thank you both again for the replies!

I do not ever think to use the debugger =(, will do my best to remember.

There was some error about std::mem_copy or something similar, so it must have been vector copying errors.