free_mlx - 1Fr3aK2/Cub3d GitHub Wiki

๐Ÿงน free_mlx

Releases all MiniLibX (MLX) graphical resources, including images, window, and display connection, to ensure proper cleanup and prevent memory leaks.

โš™๏ธ Parameters

Parameter Type Description
mlx t_mlx * Pointer to the MLX structure containing the MLX instance, window, and image data. Can be NULL.

๐Ÿ” Returns

Return value Description
void This function does not return a value. It safely releases all MLX-related resources.

๐Ÿ“– Description

The free_mlx function is responsible for safely freeing all resources associated with the MiniLibX (MLX) graphics library.
It ensures that all dynamically allocated graphical elements are destroyed in the correct order before program termination or restart.

The function performs the following steps:

  1. Checks if the mlx pointer is NULL. If so, the function returns immediately.
  2. If an image exists (mlx->img.img), it calls mlx_destroy_image() to free it and sets the pointer to NULL.
  3. If a window exists (mlx->win), it calls mlx_destroy_window() to close it and sets the pointer to NULL.
  4. If the MLX connection (mlx->mlx) exists, it calls mlx_destroy_display() to close the display connection.
  5. Finally, it frees the mlx->mlx pointer itself and sets it to NULL to avoid dangling references.

This function should always be called before exiting the program or resetting the MLX environment to prevent resource leaks and ensure a clean shutdown.

๐Ÿ’ก Example Usage

t_mlx mlx;

// Initialize MLX and create window/image resources
init_mlx(&mlx);

// Free MLX-related resources before exiting
free_mlx(&mlx);