抛出自由类型异常:读取访问冲突。脸是 nullptr

Freetype Exception thrown: read access violation. face was nullptr

''' FT_Face face = nullptr;;

FT_GlyphSlot g = face->glyph;
FT_Library ft;
if (FT_Init_FreeType(&ft))
    std::cout << "ERROR::FREETYPE: Could not init FreeType Library" <<
    std::endl;

if (FT_New_Face(ft, "fonts/arial.ttf", 0, &face))
    std::cout << "ERROR::FREETYPE: Failed to load font" << std::endl;
FT_Set_Pixel_Sizes(face, 0, 48);
if (FT_Load_Char(face, 'X', FT_LOAD_RENDER))
    std::cout << "ERROR::FREETYTPE: Failed to load Glyph" << std::en '''

当我编译程序时,出现这个错误。 “抛出未处理的异常:读取访问权限 violation.face 为 nullptr。”

问题(提到的错误)是您在程序的某处取消引用 face 而它是 nullptr,这会导致未定义的行为。

为了解决,添加一个检查以查看 face 是否为 nullptr,然后再取消引用它,如下所示:

//go inside the if block only if face is not nullptr
if(face !=nullptr)
{
    //you can safely dereference face here
}
//otherwise print a message to the console
else 
{
    //at this point face is nullptr so don't dereference it at this point
    std::cout<<"cannot dereference face "<<std::endl;
    
}

另外确保(如果尚未)face 已初始化,即它指向适当类型的对象。