al_draw_textf causing crash
William Labbett

having a problem with al_draw_textf. I'm using it and if I comment it out
the program works fine.

I've initialised the font addon and the ttf font addon.

Here's the output on the windows console.

Telling user how many files are needed for this task.
There is more than 1 file needed.
IO->font value is 0x266e1a0
al_get_display_width(IO->display) is 1920
al_get_display_width(IO->display) / 2 is 960
data.num_files_needed = 2

The code in the program is :

1if(data.num_files_needed == 1) 2 { 3 #ifdef LOAD_FILES_PROMPT_USE_COUT 4 cout << "There is 1 file needed." << endl; 5 #endif 6 al_draw_text( IO->font, WHITE, al_get_display_width(IO->display) / 2, 720, ALLEGRO_ALIGN_CENTRE, "1 file is needed for this task."); 7 } 8 else 9 { 10 #ifdef LOAD_FILES_PROMPT_USE_COUT 11 if(IO->font == NULL) 12 { 13 cout << "IO->font is NULL." << endl; 14 } 15 if(IO->display == NULL) 16 { 17 cout << "display is NULL." << endl; 18 } 19 20 cout << "There is more than 1 file needed." << endl; 21 cout << "IO->font value is " << IO->font << endl; 22 cout << "al_get_display_width(IO->display) is " << al_get_display_width(IO->display) << endl; 23 cout << "al_get_display_width(IO->display) / 2 is " << al_get_display_width(IO->display) / 2 << endl; 24 cout << "data.num_files_needed = " << data.num_files_needed << endl; 25 cout << "ALLEGRO_ALIGN_CENTRE is " << ALLEGRO_ALIGN_CENTRE << endl; 26 #endif 27 28 al_draw_textf( IO->font, WHITE, al_get_display_width(IO->display) / 2, 720, ALLEGRO_ALIGN_CENTRE, "%d files are needed for this task.", data.num_files_needed ); 29 #ifdef LOAD_FILES_PROMPT_USE_COUT 30 cout << "Called al_draw_textf." << endl; 31 #endif 32 }

I've been trying to find out the reason it doesn't crash with al_draw_textf commented out to no avail ???

Can someone help?

Chris Katko

Windows or linux?

Run in debug mode (for linux use GDB) and see what the actual crash reason is. That way you know if it's a dead pointer, or whatever. Also if there's an allegro log, read that.

Double verify that font is loaded, pointing to a real font, and that the code you gave us is exactly the code that shows the problem.

Check if a different font works, maybe you have some sort of corrupted font file and Allegro doesn't catch it.

for linux, debugging is as easy as:

gdb ./myprogram
[it will crash]
bt      ["backtrace"]

That'll show the function it died in, and what exact line of code it died on.

If you type "list" it'll show ten lines of code around where it crashed.

If it's windows, visual studio (or mingw or whatever) have their debuggers too.

William Labbett



I tried with a different font and the same crash happened.

I made a debug version, ran it in gdb and the problem didn't happen.

I noticed I'd not used the -g flag on one of the object files so I sorted that
out, ran the program in gdb from the command line and gdb didn't work.

Tried again and it worked but the problem again wasn't there.

I had the idea of running the debug version program from the icon for it in exporer and that worked too.

I thought well at least I can use my program now.

I then changed the Makefile back to how it was, built the non-debug version and it seems to be working.

I'm totally baffled :o but I'm not complaining.


Is the font loaded correctly?
There is a value for font, but is it valid?

Have you tried putting in al_draw_text instead to see if it will print at all

Edgar Reynaldo

Possibilities :
1) The font has been destroyed, but not zeroed out. This is known as a dangling pointer. It appears valid, but points to deallocated memory.

2) The current working directory is wrong. You can tell this by running it from a command console in the directory of the executable.

3) Try an absolute path to diagnose 2). If it works, the cwd is wrong.

William Labbett

Many thanks for the replies.

I load the font and then check the pointer isn't NULL. The arguments I pass to

al_load_ttf_font are valid. I don't know any other way to load the font. Seems proper to me.

I check the value of the pointer when it's loaded and it's the same value just before the crash.

Also, al_draw_text is called just before the code I posted and it works fine.

There's no call to al_destroy_font anywhere in the program.

The program works now though and I've no idea why there was a problem or what fixed it (assuming my program is really working).

If it's alright with everyone, I'll get on with the programming and put it down to a mystery.

Thanks very much for all the help.

Chris Katko

I'm always terrified when something fixes *and I don't know why* because it could happen again.

Definitely run a debugger when it crashes so you know the exact line of code (it will also include inside the Allegro code).

William Labbett

..and guess what, it has. ???

Chris Katko

If it changes randomly when you change random lines of code. It's often a pointer problem and it is or isn't breaking depending on where the compiler laid out the memory of your variables (and the pointer is, or isn't touching one of them).

Run that debugger!!!

Edgar Reynaldo

Before you go running it through a debugger. Post Code, and check that your runtime library version is the same as the header version you're using for Allegro 5.

A mismatched dll could randomly barf.

William Labbett

I'd already run that Debugger before I read the last two posts. I had the same problem as before i.e the problem didn't happen with the debugger.

Although I know that when I'm sure, I'm probably wrong, I nonetheless can't see how the lib files could be being used with the wrong headers as I'm using Siegelords 5.7.2 binaries and deps. The only thing that makes me doubt this is that I've seen
that al_is_ttf_font_initialised() is in the online manual, but I got errors when trying to use it despite havin both allegro_font.h and allero_ttf_font.h included.

Anyway, I've posted the entire project.

I'm anticipating being told I'm doing some very unnecessary things.I know it could all be simpler. It's just the way I work - I work out the way it aught to be done after doing something silly.

But say whatever.

Thanks for your perseverence.

I can't upload. Waiting to see if I can after I'd posted.

I've tried uploading the project but can't.

Mark Oates

I can't upload. Waiting to see if I can after I'd posted.

Maybe a repo or a gists might work?

Edgar Reynaldo

Have you ever had a different version of allegro 5 installed? You could be mixing dlls. Don't cross the streams, Egon!

Chris Katko

I've personally had that happen to me with DAllegro being compiled for a slightly different version of Allegro 5 blowing it up.

But if it "sometimes" crashes. ALWAYS RUN WITH THE DEBUGGER so you CATCH IT when it DOES happen again! GET THAT CALL STACK. You can't fix something when you don't even know where it's crashing.

Also, isn't there a way to make Windows/Linux programs that crash, do a "core dump"? It's a snapshot of the registers and memory when it crashed so a debugger can load it after-the-fact and look at it.



Core dumps *should* include the stack. So you can get the stack trace of what functions called what.

Thread #618663. Printed from