1

我正在使用视觉泄漏检测器寻找我的程序内存泄漏寻找我的程序内存泄漏并且找不到如何摆脱 FT_Load_Char 泄漏,文档也没有说明 GlyphSlot 的内存释放。

这是我的代码片段,其中 w 泄漏了大约 350 个字节。

// creating ascii symbol map
for (int i = 32; i < 128; i++) {
    if (FT_Load_Char(face, i, FT_LOAD_RENDER)) { // leak comes from here
        fprintf(stderr, "Loading character %c failed!\n", i);
        continue;
    }

    glTexSubImage2D(GL_TEXTURE_2D, 0, ox, oy, g->bitmap.width, g->bitmap.rows, 
        GL_ALPHA, GL_UNSIGNED_BYTE, g->bitmap.buffer);

    float ax = g->advance.x >> 6;
    float ay = ay = g->advance.y >> 6;

    float bw = g->bitmap.width;
    float bh = g->bitmap.rows;

    float bl = g->bitmap_left;
    float bt = g->bitmap_top;

    m_GlyphMap[i] = Glyph(ax,ay, bw, bh, bl, bt, ox, oy);

    ox += g->bitmap.width + 1;

    // there should be some sort of deallociation...
}

所以主要问题:是否有一些功能可以释放我缺少的 GlyphSlot?还是 Freetype 中的错误?

4

1 回答 1

4

确保调用FT_Done_FreeType(lib_); 在您关闭程序或停止使用 freetype 之后。如果不是这种情况,请确保您使用的是最新的 freetype 版本。我有几乎相同的循环,它在 Windows 8 x64 上运行良好。这是我的代码:

for (UINT32 i = 0; i < text.length(); i++) {
    err_ = FT_Load_Char(face_, text[i], FT_LOAD_RENDER);
    if (err_) {
        LOGW("Unable to select, load and render character."
            " Error code: %d", err_);
        continue;
    }
    FT_Bitmap bitmap = glyphSlot->bitmap;
    FT_UInt glyphIndex = FT_Get_Char_Index(face_, text[i]);
    err_ = FT_Get_Kerning(face_, previous, glyphIndex,
        FT_KERNING_DEFAULT, &delta);
    if (err_) {
        LOGW("Unable to get kerning for character."
            " Error code: %d", err_);
        continue;
    }
    Glyph tmp;
    tmp.kerningOffset = delta.x >> 6;
    tmp.buffer = new UINT8[bitmap.rows * bitmap.width];
    memcpy(tmp.buffer, bitmap.buffer, bitmap.rows * bitmap.width);
    tmp.height = bitmap.rows;
    tmp.width = bitmap.width;
    tmp.offsetLeft = glyphSlot->bitmap_left;
    if (tmp.offsetLeft < 0) {
        tmp.offsetLeft = 0;
    }
    tmp.offsetTop = glyphSlot->bitmap_top;
    tmp.advanceX = glyphSlot->advance.x >> 6;
    tmp.advanceY = glyphSlot->advance.y >> 6;
    glyphs.push_back(tmp);
    previous = glyphIndex;
    width += tmp.advanceX + tmp.kerningOffset;
}

如果您单独分配符号缓冲区,也不要忘记删除它们:

for (SIZE i = 0; i < glyphs.size(); i++) {
    Glyph g = glyphs[i];
    delete [] g.buffer;
}
于 2013-09-28T08:26:56.750 回答