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:
- Checks if the
mlxpointer isNULL. If so, the function returns immediately. - If an image exists (
mlx->img.img), it callsmlx_destroy_image()to free it and sets the pointer toNULL. - If a window exists (
mlx->win), it callsmlx_destroy_window()to close it and sets the pointer toNULL. - If the MLX connection (
mlx->mlx) exists, it callsmlx_destroy_display()to close the display connection. - Finally, it frees the
mlx->mlxpointer itself and sets it toNULLto 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);