Search code examples
csegmentation-faultfreetype

Segfault on very simple source


I'm trying to write a simple script using the FreeType library. The segfault is occurring during execution of the FT_Set_Pixel_Sizes method, though I'm using it correctly. Any help would be great. Here's the full code:

#include <ft2build.h>
#include FT_FREETYPE_H

main() {

    FT_Library library;
    FT_Face face;
    FT_GlyphSlot slot;
    FT_UInt glyph_index = 30;
    char* font_file = "/usr/share/fonts/truetype/freefont/FreeMono.ttf";

    // Render font
    FT_New_Face(library, font_file, 0, &face);

    FT_Set_Pixel_Sizes(face, 0, 16); /* THIS LINE IS CAUSING THE SEGFAULT */

    slot = face->glyph;
    FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
    FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL);
}

Solution

  • You did not initialize your Library variable : see FT_LIBRARY documentation. You should use FT_Init_FreeType :

    FT_Init_FreeType

    Defined in FT_FREETYPE_H (freetype/freetype.h).

    FT_EXPORT( FT_Error ) FT_Init_FreeType( FT_Library *alibrary );

    Initialize a new FreeType library object. The set of modules that are registered by this function is determined at build time.

    output alibrary A handle to a new library object.

    return FreeType error code. 0 means success.

    You could first get used to this library following this tutorial. Take care to check the return values too ...