Before call FT_New_Face
, are you sure the font->_face
was new? So, you need new a FT_Face
if font->_face
is NULL.
Note: It is not necessary to init a FT_Library
for each Font instance. You can make the Font::_ft
to be static.
Finally, code should be:
class Font
{
public:
static FT_Library _ft;
static Font* create(const char* path, unsigned int size)
{
Font* font = new Font();
if (Font::_ft == NULL)
if (FT_Init_FreeType(&Font::_ft))
return NULL;
if (font->_face == NULL)
font->_face = new FT_Face;
if (FT_New_Face(font->_ft, path, 0, font->_face)) {
std::cerr << "Font: Could not load font from file " << path << "." << std::endl;
return NULL;
}
FT_Set_Pixel_Sizes(*(font->_face), 0, size);
return font;
}
private:
// Set the member - _face to NULL when call constructor
Font() : _face(NULL) {}
~Font() { /* release the memory */ }
FT_Face* _face;
};
// Set the static member - _ft to NULL
FT_Library Font::_ft = NULL;
At last, you need uninitialize/release all memory about FreeType2 when call destructor.
Note: The fourth variable (FT_Face
) of FT_New_Face
must be instance. The FT_New_Face
don't allocate the memory for FT_Face
.