1

当使用带有;的Hans Kendrick字体时SDL2_ttf

#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>

// Very shortened for demo purposes
int main () {
    if (SDL_Init(SDL_INIT_VIDEO) != 0) { return 1; }
    if (TTF_Init() == -1) { return 1; }

    TTF_Font *font = TTF_OpenFont("font.ttf", 28);

    if (font == NULL) { return 1; }

    SDL_Color blank = {0, 0, 0, 0};
    SDL_Color colour = {0, 0, 0, 0};

    TTF_RenderUTF8_Shaded(font, "Y", colour, blank);
}

valgrind报告错误;

==18109== Invalid read of size 1
==18109==    at 0x535812C: TTF_RenderUTF8_Shaded (in /usr/lib/libSDL2_ttf-2.0.so.0.10.2)
==18109==    by 0x40080B: main (in /home/joshua/fontbug/test)
==18109==  Address 0xd9aaf4f is 1 bytes before a block of size 560 alloc'd
==18109==    at 0x4C28730: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==18109==    by 0x4ED91FB: ??? (in /usr/lib/libSDL2-2.0.so.0.2.1)
==18109==    by 0x5357F00: TTF_RenderUTF8_Shaded (in /usr/lib/libSDL2_ttf-2.0.so.0.10.2)
==18109==    by 0x40080B: main (in /home/joshua/fontbug/test)

和一些系统崩溃。这取决于字符(前面有空格的字符串从来没有这个问题)和可能的字体大小。

使用 DejaVu 没有这个问题。

是什么原因造成的,如何预防?


构建:

g++ -o test main.cpp $(sdl2-config --cflags --libs) -lSDL2_ttf
4

1 回答 1

0

我认为(= 不是 100% 肯定)在尝试渲染任何东西之前你必须有一个渲染器。为什么不尝试在使用 TTF_RenderUTF8_Shaded 之前使用 SDL_CreateWindowAndRenderer ?

于 2014-10-03T13:46:33.643 回答