memononen / nanovg

Antialiased 2D vector drawing library on top of OpenGL for UI and visualizations.
zlib License
5.15k stars 770 forks source link

Memory leak with freetype fonts [fix] #657

Open Youlean opened 1 year ago

Youlean commented 1 year ago

I have noticed that the freetype fonts don't release their memory correctly.

The fix would be:

void fonsDeleteInternal(FONScontext* stash)
{
    int i;
    if (stash == NULL) return;

    if (stash->params.renderDelete)
        stash->params.renderDelete(stash->params.userPtr);

    for (i = 0; i < stash->nfonts; ++i)
        fons__tt_done_face(&stash->fonts[i]->font);

    for (i = 0; i < stash->nfonts; ++i)
        fons__freeFont(stash->fonts[i]);

    if (stash->atlas) fons__deleteAtlas(stash->atlas);
    if (stash->fonts) free(stash->fonts);
    if (stash->texData) free(stash->texData);
    if (stash->scratch) free(stash->scratch);
    fons__tt_done(stash);
    free(stash);
}
int fons__tt_done_face(FONSttFontImpl* font)
{
    FT_Error ftError;
    ftError = FT_Done_Face(font->font);
    return ftError == 0;
}
tazok81 commented 8 months ago

Hi, I tried add this to my project, but FT_Done_Face is not implemented. What it does?

mulle-nat commented 8 months ago

There are two font libraries used by nanovg, if you use STB (the default) you don't need it.

tazok81 commented 8 months ago

thank you

Youlean commented 8 months ago

Indeed. If you use STB, you might want to increase font scale so that you can get better font rendering on low DPI screens