image: fix allocator tracking; prevent double-free on release
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: Ia24b68cd4850a7749cd10643e5423b646a6a6964
This commit is contained in:
parent
5ba4407367
commit
dc9f159470
1 changed files with 8 additions and 3 deletions
11
src/image.c
11
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,
|
image->data = stbi_load(path, &image->width, &image->height, &image->channels,
|
||||||
desired_channels);
|
desired_channels);
|
||||||
image->channels = desired_channels;
|
image->channels = desired_channels;
|
||||||
|
image->data_from_stbi = true;
|
||||||
if (!image->data) {
|
if (!image->data) {
|
||||||
chroma_log("ERROR", "Failed to load image %s: %s", path,
|
chroma_log("ERROR", "Failed to load image %s: %s", path,
|
||||||
stbi_failure_reason());
|
stbi_failure_reason());
|
||||||
|
|
@ -227,6 +228,7 @@ int chroma_image_load(chroma_image_t *image, const char *path,
|
||||||
|
|
||||||
stbi_image_free(image->data);
|
stbi_image_free(image->data);
|
||||||
image->data = downsampled_data;
|
image->data = downsampled_data;
|
||||||
|
image->data_from_stbi = false;
|
||||||
image->width = optimal_width;
|
image->width = optimal_width;
|
||||||
image->height = optimal_height;
|
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);
|
chroma_log_resource_deallocation("image_data", image_size, image->path);
|
||||||
|
|
||||||
// Always use stbi_image_free since we load directly with stbi_load
|
if (image->data_from_stbi) {
|
||||||
stbi_image_free(image->data);
|
stbi_image_free(image->data);
|
||||||
|
} else {
|
||||||
|
free(image->data);
|
||||||
|
}
|
||||||
image->data = NULL;
|
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
|
// Release a reference to an image; free when ref_count reaches zero
|
||||||
void chroma_image_release(chroma_image_t *image) {
|
void chroma_image_release(chroma_image_t *image) {
|
||||||
if (!image) {
|
if (!image || !image->loaded) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue