c - 非常简单的源上的段错误

标签 c segmentation-fault freetype

我正在尝试使用 FreeType 库编写一个简单的脚本。段错误是在执行 FT_Set_Pixel_Sizes 方法期间发生的,尽管我正确使用了它。任何帮助都会很棒。这是完整的代码:

#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);
}

最佳答案

您没有初始化您的 Library 变量:参见 FT_LIBRARY documentation .你应该使用 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.

您可以先习惯这个库,然后再使用 tutorial .也要注意检查返回值...

关于c - 非常简单的源上的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12334326/

相关文章:

c - C中结构体的实际大小是多少

c++ - 静态内存使用的层次概述

c - 为什么 C 程序给出不同的结果?

c - 由于分配问题导致段错误?

mysql - 访问 MySQL 数据库时出现 RoR 段错误

c++ - 计算 FreeType 字形渲染的位置?

c++ - FreeType OpenGL 动态文本 = 糟糕的性能

c - sqlite 中的表名长度会影响性能。为什么?

linux - 段错误处理程序中的段错误

php - PHP 中的 FreeType 扩展