image: fix allocator tracking; prevent double-free on release

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: Ia24b68cd4850a7749cd10643e5423b646a6a6964
This commit is contained in:
raf 2026-05-01 14:08:16 +03:00
commit dc9f159470
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF

View file

@ -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;
}