2

I'm writing a program that renders font in SDL. I have a struct with the color and text of a 'pixel.' It causes a segmentation fault, and I can't make any sense of it.

struct pixInfo
{
    SDL_Color bColor;
    SDL_Color tColor;
    Uint16 ch;
};

And this is the rendering code

fchar = TTF_RenderGlyph_Solid(com->font, im->ch, im->tColor); // Segmentation fault

com is a pointer to the main class, which stores the font. im is an iterator to a pixInfo struct fchar is a SDL_Surface* This will crash if im->ch is something like 9554 (ASCI characters work fine)

However, This code does work

fchar = TTF_RenderGlyph_Solid(com->font, 9554, im->tColor);
// OR
Uint16 num = 9554;
fchar = TTF_RenderGlyph_Solid(com->font, num, im->tColor);

This does not work:

Uint16 num = im->ch;
fchar = TTF_RenderGlyph_Solid(com->font, num, im->tColor); // Segmentation fault

If I print the 'num' variable to the output in both of those examples, they are 9554 (Keep in mind, it's not just 9554 doing this)

EDIT: I just tried another approach, it segfaults as well

Uint16 num;
memcpy(&num, &im->ch, sizeof(Uint16));
fchar = TTF_RenderGlyph_Solid(com->font, num, im->tColor); // Segmentation fault

EDIT 2: This code does not crash

Uint16 num;
memcpy(&num, &im->ch, sizeof(Uint16));

if (num == 9554)
{
    std::cout << "9554" << std::endl;
    num = 32;
}

fchar = TTF_RenderGlyph_Solid(com->font, num, im->tColor);

But this code does segfault

Uint16 num;
memcpy(&num, &im->ch, sizeof(Uint16));

if (num == 9554)
{
    std::cout << "9554" << std::endl;
    num = 32;
    num = 9554;
}

fchar = TTF_RenderGlyph_Solid(com->font, num, im->tColor);
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
user3103398
  • 143
  • 9
  • It all causes a segmentation fault. The pixInfo structures are allocated in a std::vector inside of an std::vector. As I said, I can print the variables from it just fine – user3103398 Sep 26 '14 at 21:06
  • Does the font contain a glyph for [U+2552 "╒"](http://www.fileformat.info/info/unicode/char/2552/index.htm)? – Jongware Sep 26 '14 at 21:32
  • Jonhware, yes. As I said, I can render it by manually putting 9554 as the second argument – user3103398 Sep 26 '14 at 21:33
  • 1
    Is this with the latest stable SDL, 2.0.3? In that, a single UInt16 character is translated to a short UTF8 string, which then gets rendered through [`TTF_RenderUTF8_Solid`](https://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf_44.html#SEC44). Can you try if that function works? Also, does `FindGlyph` in itself load the character? – Jongware Sep 26 '14 at 22:21
  • Jongware, I just tried it with TTF_RenderUTF8_Solid... Same result. I am using SDL 2.0.3 – user3103398 Sep 26 '14 at 22:22
  • The whole project is on github if you're interested. https://github.com/alakazard12/OCEmulator The specific function having trouble is src/screencomponent.cpp line 121 – user3103398 Sep 26 '14 at 22:29
  • There doesn't seem to be a problem with the `TTF_Render..` functions (I got them working), and the snippets of code you provide above work. This suggests the error is somewhere else. Try freeing your `fchar` surfaces: "The caller (you!) is responsible for freeing any returned surface." – Jongware Sep 27 '14 at 23:08

0 Answers0