Fix memory leak in DrawUtils::print function

This commit is contained in:
Maschell 2023-01-05 21:58:20 +01:00
parent e951299ea9
commit a2081206ed

View File

@ -1,6 +1,7 @@
#include "DrawUtils.h" #include "DrawUtils.h"
#include "logger.h" #include "logger.h"
#include "utils.h"
#include <coreinit/cache.h> #include <coreinit/cache.h>
#include <coreinit/memory.h> #include <coreinit/memory.h>
#include <coreinit/screen.h> #include <coreinit/screen.h>
@ -313,11 +314,12 @@ void DrawUtils::print(uint32_t x, uint32_t y, const wchar_t *string, bool alignR
textureHeight = 4; textureHeight = 4;
} }
img.pixels = malloc(img.width * img.height); auto buffer = make_unique_nothrow<uint8_t[]>((uint32_t) (img.width * img.height));
if (!img.pixels) { if (!buffer) {
DEBUG_FUNCTION_LINE_ERR("Failed to allocate memory for glyph"); DEBUG_FUNCTION_LINE_ERR("Failed to allocate memory for glyph");
return; return;
} }
img.pixels = buffer.get();
if (sft_render(&pFont, gid, img) < 0) { if (sft_render(&pFont, gid, img) < 0) {
DEBUG_FUNCTION_LINE_ERR("Failed to render glyph"); DEBUG_FUNCTION_LINE_ERR("Failed to render glyph");
return; 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)); draw_freetype_bitmap(&img, (int32_t) (penX + mtx.leftSideBearing), (int32_t) (penY + mtx.yOffset));
penX += (int32_t) mtx.advanceWidth; penX += (int32_t) mtx.advanceWidth;
} }
free(img.pixels);
} }
} }
} }