From 4e42b6c659063f5587cb6f8fe3437f6f1c58aa65 Mon Sep 17 00:00:00 2001 From: Olli Wang Date: Sat, 26 Mar 2022 23:10:06 +0800 Subject: [PATCH] Fixes memory leaks due to not releasing font images properly. --- src/nanovg.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/nanovg.c b/src/nanovg.c index 5d0cb4c..61826e8 100644 --- a/src/nanovg.c +++ b/src/nanovg.c @@ -28,7 +28,7 @@ #ifndef NVG_NO_STB #define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" -#endif +#endif #ifdef _MSC_VER #pragma warning(disable: 4100) // unreferenced formal parameter @@ -396,6 +396,7 @@ void nvgEndFrame(NVGcontext* ctx) ctx->params.renderFlush(ctx->params.userPtr); if (ctx->fontImageIdx != 0) { int fontImage = ctx->fontImages[ctx->fontImageIdx]; + ctx->fontImages[ctx->fontImageIdx] = 0; int i, j, iw, ih; // delete images that smaller than current one if (fontImage == 0) @@ -404,20 +405,19 @@ void nvgEndFrame(NVGcontext* ctx) for (i = j = 0; i < ctx->fontImageIdx; i++) { if (ctx->fontImages[i] != 0) { int nw, nh; - nvgImageSize(ctx, ctx->fontImages[i], &nw, &nh); + int image = ctx->fontImages[i]; + ctx->fontImages[i] = 0; + nvgImageSize(ctx, image, &nw, &nh); if (nw < iw || nh < ih) - nvgDeleteImage(ctx, ctx->fontImages[i]); + nvgDeleteImage(ctx, image); else - ctx->fontImages[j++] = ctx->fontImages[i]; + ctx->fontImages[j++] = image; } } // make current font image to first - ctx->fontImages[j++] = ctx->fontImages[0]; + ctx->fontImages[j] = ctx->fontImages[0]; ctx->fontImages[0] = fontImage; ctx->fontImageIdx = 0; - // clear all images after j - for (i = j; i < NVG_MAX_FONTIMAGES; i++) - ctx->fontImages[i] = 0; } }