Thread: SDL: TTF_RenderText_Solid screwing up in Ubuntu 9.10

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    62

    *SOLVED* SDL: TTF_RenderText_Solid screwing up in Ubuntu 9.10

    Hey guys, I've been teaching myself SDL to start out w/ a simple API, using LazyFoo's tutorials. I created a Tic Tac Toe program, which works great in Windows XP, under Code::Blocks. When I tried running the code in Ubuntu 9.10 under Code::Blocks I get a segmentation fault. I recalled reading that TTF_RenderText_Solid may not work under linux, so I tried using both RenderText_Shaded and RenderText_Blended, but wound up getting the same error...this is the chunk of code I believe to be the culprit.

    Code:
    void printScoreBoard(){
        int relwidth = (SCREENWIDTH - scores->w)/2; // FOR BUTTONS
        int relheight = (SCREENHEIGHT - scores->h)/2;
        int coords[3] = {relwidth+54,relwidth+115,relwidth+182};
        char buffer[5] = {'\0','\0','\0','\0','\0'};
        redrawBoard();
        applySurface(relwidth,relheight,scores,screen);
        for (int i = 0; i < 3; i++){
            sprintf(buffer,"%d",scoreboard[i]); // Scoreboard is a var holding wins and whatnot
            if (!(message = TTF_RenderText_Blended(font,buffer,textColor)))
                printf("Oh My Goodness, an error : %s", TTF_GetError());
            else
                applySurface(coords[i],relheight+209,message,screen);
        } // CLSP is a constant specifying the location of the button to clear scores in the sprite
        applySurface(relwidth+35,relheight+268,buttons,screen,&mclip[CLSP]);
    }
    The disassembly of memory shows the crash happening here:
    Code:
    0x80495fe	mov    eax,DWORD PTR [ebp+eax*4-0x28]
    if that's of any use...

    Essentially the C++ code is supposed to print the "score" of the current game, based on X wins, O wins, and Ties, using TTF fonts to display the score...

    Here's some screenshots
    http://i93.photobucket.com/albums/l5...an/screen2.jpg
    http://i93.photobucket.com/albums/l5...an/screen3.jpg

    I read the TTF documentation on the functions, which say that passing a NULL font can cause a segfault, and that passing NULL text causes undefined behavior, but I defined the font, and I'm linking against SDL_ttf and freetype...so I'm at a complete loss...does anyone know a workaround or something that I can use?

    I attached the full cpp file, it was 342 lines so I didn't think it was really good etiquette to paste it all here ^_^
    Last edited by mkylman; 03-05-2010 at 01:07 AM. Reason: Solved thanks to Salem and _Mike

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    I tried running your game under mandriva cooker (I just copied some random pngs to the Resource folder) and it works fine, so there must be something wrong with your font. Test the font var for NULL before you use it.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    I do that in the cpp file, if you look at the function loadFiles(), that was why I was so confused >.<
    I assume you clicked the Scores button in the menu? I'll upload some files from the resources folder
    Rename mystik.txt to mystik.txt

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if (!(message = TTF_RenderText_Blended(font,buffer,textColor)))
    So use the debugger, put a breakpoint on this line and examine in detail the passed parameters.

    Or wait for the crash to happen (whilst running in the debugger), then examine the variables in this stack frame.

    > char buffer[5]
    Scores > 9999 perhaps?
    This is an easy buffer overflow.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by Salem View Post
    > char buffer[5]
    Scores > 9999 perhaps?
    This is an easy buffer overflow.
    And it is indeed overflowing. The reason I didn't notice any crash when testing was because I was compiling for 64-bit and the compiler adds enough alignment padding after the buffer variable to prevent any stack corruption.

    In setClips():
    Code:
    // Menu buttons width and height, along w/ victory messages
    for (int i = 0; i < 8; i++){
        mclip[i].w = vclip[i].w = 180;
        mclip[i].h = vclip[i].h = 55;
    }
    (hint: check the size of the vclip array)
    This causes a buffer overflow which spills over in to the scoreboard[] array, which causes a buffer overflow in buffer[] in printScoreBoard(), which causes stack corruption.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    *facepalms self*
    I can't believe I did something that stupid. I've never encountered an issue like this before so I didn't really give it any thought. This is a perfect of example of me trying to make my code more "efficient" and actually screwing it up.

    God I feel stupid now -_-

    Thank you so much guys, I was ripping my hair out!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SDL project setup
    By rogster001 in forum C Programming
    Replies: 22
    Last Post: 08-28-2009, 08:05 AM
  2. Problems compiling this SDL app
    By Rider in forum C++ Programming
    Replies: 3
    Last Post: 03-27-2007, 12:22 PM
  3. SDL: Garbage collection (?) screwing me
    By Brian in forum Game Programming
    Replies: 4
    Last Post: 05-08-2005, 09:13 AM
  4. SDL and Windows
    By nickname_changed in forum Windows Programming
    Replies: 14
    Last Post: 10-24-2003, 12:19 AM
  5. sdl in c++
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-07-2002, 07:46 AM