我正在使用OpenGL库在FreeType2应用程序中实现字体呈现。我的问题是,初始化多个FT_Face对象会导致程序崩溃。我尝试使用FT_Library的单个实例,并为每个新字体调用FT_Init_FreeType()。我还尝试为每种字体设置单独的FT_Library实例。这两种初始化都能工作,但是当我下次使用new运算符时,无论在哪里,如果在下一行代码上,我都会从new获得断言。
下面是FreeType2库初始化的字体对象创建方法:
Font::Font* create(const char* path, unsigned int size) {
Font* font = new Font();
FT_Init_FreeType(&(font->_ft));
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);
}如果我叫它一次的话,这段代码就能正常工作了。如果我第二次调用它,然后在程序的另一部分中使用new创建对象,那么应用程序就会崩溃。
这里怎么了?应该有一些好方法来加载几个字体..。
断言消息:malloc.c:2365: sysmalloc: Assertion (old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
更新:
字体类声明,每个类包含ft库实例的版本:
class Font
{
public:
static Font* create(const char* font, unsigned int size);
void renderText(const wchar_t* text, float x, float y, float sx, float sy);
private:
Font();
~Font();
FT_Library _ft;
FT_Face* _face;
};renderText()方法基本使用_face来查看所需的字符并呈现它们。
发布于 2014-02-05 09:23:43
在打电话给FT_New_Face之前,您确定font->_face是新的吗?因此,如果FT_Face为NULL,则需要新的font->_face。
注意事项:没有必要为每个字体实例插入一个FT_Library。您可以使Font::_ft成为静态的。
最后,代码应该是:
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;最后,在调用析构函数时,需要取消初始化/释放有关FreeType2的所有内存。
注意事项:FT_New_Face的第四个变量(FT_Face)必须是实例。FT_New_Face不为FT_Face分配内存。
https://stackoverflow.com/questions/21517421
复制相似问题