Skip to content

Commit

Permalink
Merge pull request #30 from pajotg/fix_delete_instances
Browse files Browse the repository at this point in the history
[Bug Fix] Segfaults/UB when deleting images with 2 or more instances
  • Loading branch information
W2Wizard authored May 16, 2022
2 parents 053ab7e + a639d68 commit 2418d26
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/mlx_images.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: W2Wizard <[email protected]> +#+ */
/* +#+ */
/* Created: 2022/01/21 15:34:45 by W2Wizard #+# #+# */
/* Updated: 2022/04/28 14:44:28 by lde-la-h ######## odam.nl */
/* Updated: 2022/05/15 20:57:16 by jsimonis ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -97,7 +97,7 @@ void mlx_set_instance_depth(mlx_instance_t* instance, int32_t zdepth)
return;
instance->z = zdepth;

/**
/**
* NOTE: The reason why we don't sort directly is that
* the user might call this function multiple times in a row and we don't
* want to sort for every change. Pre-loop wise that is.
Expand All @@ -109,7 +109,7 @@ int32_t mlx_image_to_window(mlx_t* mlx, mlx_image_t* img, int32_t x, int32_t y)
{
MLX_ASSERT(!mlx);
MLX_ASSERT(!img);

// Allocate buffers...
mlx_instance_t* temp = realloc(img->instances, (++img->count) * sizeof(mlx_instance_t));
draw_queue_t* queue = calloc(1, sizeof(draw_queue_t));
Expand All @@ -123,7 +123,7 @@ int32_t mlx_image_to_window(mlx_t* mlx, mlx_image_t* img, int32_t x, int32_t y)
img->instances[index].x = x;
img->instances[index].y = y;

// NOTE: We keep updating the Z for the convenience of the user.
// NOTE: We keep updating the Z for the convenience of the user.
// Always update Z depth to prevent overlapping images by default.
img->instances[index].z = ((mlx_ctx_t*)mlx->context)->zdepth++;

Expand Down Expand Up @@ -186,11 +186,14 @@ void mlx_delete_image(mlx_t* mlx, mlx_image_t* image)
MLX_ASSERT(!image);

mlx_ctx_t* mlxctx = mlx->context;
mlx_list_t* imglst = mlx_lstremove(&mlxctx->images, image, &mlx_equal_image);
mlx_list_t* quelst = mlx_lstremove(&mlxctx->render_queue, image, &mlx_equal_inst);
if (quelst)

// Delete all instances in the render queue
mlx_list_t* quelst;
while ((quelst = mlx_lstremove(&mlxctx->render_queue, image, &mlx_equal_inst)))
mlx_freen(2, quelst->content, quelst);
if (imglst)

mlx_list_t* imglst;
if ((imglst = mlx_lstremove(&mlxctx->images, image, &mlx_equal_image)))
{
glDeleteTextures(1, &((mlx_image_ctx_t*)image->context)->texture);
mlx_freen(5, image->pixels, image->instances, image->context, imglst, image);
Expand Down

0 comments on commit 2418d26

Please sign in to comment.