From dc9f159470cb818cdddff1cd0dd6ee083be50eed Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Fri, 1 May 2026 14:08:16 +0300 Subject: [PATCH] image: fix allocator tracking; prevent double-free on release Signed-off-by: NotAShelf Change-Id: Ia24b68cd4850a7749cd10643e5423b646a6a6964 --- src/image.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/image.c b/src/image.c index ef63c4c..a424a7e 100644 --- a/src/image.c +++ b/src/image.c @@ -143,6 +143,7 @@ int chroma_image_load(chroma_image_t *image, const char *path, image->data = stbi_load(path, &image->width, &image->height, &image->channels, desired_channels); image->channels = desired_channels; + image->data_from_stbi = true; if (!image->data) { chroma_log("ERROR", "Failed to load image %s: %s", path, stbi_failure_reason()); @@ -227,6 +228,7 @@ int chroma_image_load(chroma_image_t *image, const char *path, stbi_image_free(image->data); image->data = downsampled_data; + image->data_from_stbi = false; image->width = optimal_width; image->height = optimal_height; @@ -271,8 +273,11 @@ void chroma_image_free(chroma_image_t *image) { chroma_log_resource_deallocation("image_data", image_size, image->path); - // Always use stbi_image_free since we load directly with stbi_load - stbi_image_free(image->data); + if (image->data_from_stbi) { + stbi_image_free(image->data); + } else { + free(image->data); + } image->data = NULL; } @@ -291,7 +296,7 @@ void chroma_image_free(chroma_image_t *image) { // Release a reference to an image; free when ref_count reaches zero void chroma_image_release(chroma_image_t *image) { - if (!image) { + if (!image || !image->loaded) { return; }