From a2081206ed15ab044739f4f8dd3a65e211b1943f Mon Sep 17 00:00:00 2001 From: Maschell Date: Thu, 5 Jan 2023 21:58:20 +0100 Subject: [PATCH] Fix memory leak in DrawUtils::print function --- source/utils/DrawUtils.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/source/utils/DrawUtils.cpp b/source/utils/DrawUtils.cpp index e89b06f..ca8ef4c 100644 --- a/source/utils/DrawUtils.cpp +++ b/source/utils/DrawUtils.cpp @@ -1,6 +1,7 @@ #include "DrawUtils.h" #include "logger.h" +#include "utils.h" #include #include #include @@ -313,11 +314,12 @@ void DrawUtils::print(uint32_t x, uint32_t y, const wchar_t *string, bool alignR textureHeight = 4; } - img.pixels = malloc(img.width * img.height); - if (!img.pixels) { + auto buffer = make_unique_nothrow((uint32_t) (img.width * img.height)); + if (!buffer) { DEBUG_FUNCTION_LINE_ERR("Failed to allocate memory for glyph"); return; } + img.pixels = buffer.get(); if (sft_render(&pFont, gid, img) < 0) { DEBUG_FUNCTION_LINE_ERR("Failed to render glyph"); return; @@ -325,7 +327,6 @@ void DrawUtils::print(uint32_t x, uint32_t y, const wchar_t *string, bool alignR draw_freetype_bitmap(&img, (int32_t) (penX + mtx.leftSideBearing), (int32_t) (penY + mtx.yOffset)); penX += (int32_t) mtx.advanceWidth; } - free(img.pixels); } } }