# SDL_ttf weirdness

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

## Recommended Posts

Read a couple of threads here about problems installing SDL_ttf. I am pretty sure I have it installed correctly. The call to TTF_Init() returns 0, which is correct. Problem is, I'm gettingwhat looks like a NULL reference error on the call to TTF_OpenFont; The error says: Access violation reading location 0x00000000 The offending code:
/* Initialize the TTF library */
if ( TTF_Init() < 0 ) {
fprintf(stderr, "Couldn't initialize TTF: %s\n",SDL_GetError());
return(2);
}

atexit(TTF_Quit);
atexit(SDL_Quit);

// error is generated by this line:
TTF_Font *font = TTF_OpenFont("arial.ttf", 20);
if ( font == NULL ) {
fprintf(stderr, "Couldn't load 22 pt font from arial.ttf: %s\n", SDL_GetError());
return(2);
}


The file arial.ttf IS in the program directory, as well as sdl_ttf.dll. The libs are linked in also. As I said, the call to TTF_Init() is fine, so I assume that if the lib weren't linked, that call would not work at all. The debugger also seems to know that 'font' is an TTF_Font struct, but lists its value as "0x00000000" Any ideas? I'm pretty well stumped here. Thanks in advance.

##### Share on other sites
Incidentally, the showfont proggy that comes with SDL_ttf runs just fine. I can't see any significant differences between what that program is doing, and what I'm doing, though.

##### Share on other sites
I can't see anything wrong. That's certainly how I load in my text. Just to test, use the TTF_WasInit() function right before the call to TTF_OpenFont() just to make sure something didn't happen in between... if there is an inbetween.

##### Share on other sites
The only thing I can think of is that maybe your font file has a problem with working at 20pt? The showfont program doesn't display it at 20pt, does it? I can't recall.

Try loading it at 10 or 12 or something, to see if it makes any difference.

Ryan

##### Share on other sites
Quote:
 Original post by Ryan ClarkThe only thing I can think of is that maybe your font file has a problem with working at 20pt? The showfont program doesn't display it at 20pt, does it? I can't recall.Try loading it at 10 or 12 or something, to see if it makes any difference.Your code looks fine though...Ryan

It's not loading at all, that's the problem. It crashes before it executes the call to TTF_OpenFont(). That is to say, it breaks at that line, and trying to step further leaves it on that line with the same error.

I even tried doing:

if((font = TTF_LoadFont("arial.ttf", 20)) == NULL)

but it still won't execute that line at all.

I have managed to create an empty main() function from another test example with nothing but the SDL and TTF init code in it, with a single call to Open font. That works fine.

I guess I will start pasting in my other stuff until I figure out why it's dying. Not real happy about this, since I already have 10-12 classes, and a couple of thousand lines of code, but I can't think of what else to try at this point.

##### Share on other sites
It's a bug in SDL_TTF. I've sent in a patch that fixes it, but it hasn't been applied yet.
Here's how you can work around the problem:
SDL_RWops *rw=SDL_RWFromFile("arial.ttf","rb");if(!rw){    fprintf(stderr, "Couldn't load 22 pt font from arial.ttf: %s\n", SDL_GetError());    return(2);}TTF_Font *font = TTF_OpenFontRW(rw,1,20);if ( font == NULL ) {    fprintf(stderr, "Couldn't load 22 pt font from arial.ttf: %s\n", SDL_GetError());    return(2);}

##### Share on other sites
Quote:
 Original post by TravisWellsIt's a bug in SDL_TTF. I've sent in a patch that fixes it, but it hasn't been applied yet.

##### Share on other sites
Quote:
 Original post by TravisWellsIt's a bug in SDL_TTF. I've sent in a patch that fixes it, but it hasn't been applied yet. Here's how you can work around the problem:SDL_RWops *rw=SDL_RWFromFile("arial.ttf","rb");if(!rw){ fprintf(stderr, "Couldn't load 22 pt font from arial.ttf: %s\n", SDL_GetError()); return(2);}TTF_Font *font = TTF_OpenFontRW(rw,1,20);if ( font == NULL ) { fprintf(stderr, "Couldn't load 22 pt font from arial.ttf: %s\n", SDL_GetError()); return(2);}

Oddly enough, pasting everything into the aforementioned empty project seems to have fixed it. I will keep the RWops thing in mind, tho.

##### Share on other sites
Quote:

It's a problem with TTF_OpenFont. It's just a wrapper function, basicly the same code I posted, minus the "if(!rw)" part.
So if you try to open a font, and the file is missing/unreadable, a NULL pointer gets passed to TTF_OpenFontRW, which assumes the pointer isn't NULL, and it segfaults.

I've submitted a fix for it, I don't know if it's been applied to the CVS yet (CVS for SDL_TTF seems to be down right now)

vHaB: I bet what was happening was you had your paths messed up somehow, so your original project wasn't finding arial.ttf and triggering the bug. Remaking the project fixed the pathing, so the bug isn't triggered.

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
JoeJ
12

• 14
• 22
• 9
• 31
• 18
• ### Forum Statistics

• Total Topics
632617
• Total Posts
3007475
• ### Who's Online (See full list)

There are no registered users currently online

×