removed leaks

This commit is contained in:
Theo Champion 2025-06-25 18:08:19 +02:00
parent c1484d2d3c
commit 2c009b5469
10 changed files with 67 additions and 31 deletions

View file

@ -6,7 +6,7 @@
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/06 13:16:11 by tchampio #+# #+# */
/* Updated: 2025/06/24 11:50:17 by tchampio ### ########.fr */
/* Updated: 2025/06/25 18:07:40 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -45,37 +45,25 @@ void draw_2d_wall(unsigned int color, t_mlx_data *data,
}
}
void free_tab(char **tab)
int destroy(t_cub3d_data *data)
{
int i;
if (!tab)
return ;
i = 0;
while (tab[i])
{
free(tab[i]);
i++;
}
free(tab);
}
int destroy(t_mapdata *map)
{
free_tab(map->map);
free_tab(map->mapflood);
free(map->ea_texture);
free(map->no_texture);
free(map->so_texture);
free(map->we_texture);
free(map->filename);
free_map(data->map);
if (data->mlx_win)
mlx_destroy_window(data->mlx, data->mlx_win);
if (data->mlx_data)
mlx_destroy_image(data->mlx, data->mlx_data->img);
free(data->mlx_data);
if (data->mlx)
mlx_destroy_display(data->mlx);
free(data->mlx);
exit(0);
return 0;
}
int key_destroy(int keycode, t_mapdata *map)
int key_destroy(int keycode, t_cub3d_data *data)
{
if (keycode == 65307)
destroy(map);
destroy(data);
return (0);
}
@ -111,11 +99,12 @@ int main(int argc, char **argv)
return (ft_printf("Error: Wrong map file. Reason: %s\n", data.map->error), 1);
data.mlx = mlx_init();
data.mlx_win = mlx_new_window(data.mlx, 800, 600, "Cub3d");
data.mlx_data = ft_calloc(sizeof(t_mlx_data), 1);
data.mlx_data->img = mlx_new_image(data.mlx, 800, 600);
data.mlx_data->addr = mlx_get_data_addr(data.mlx_data->img, &data.mlx_data->bits_per_pixel, &data.mlx_data->line_length, &data.mlx_data->endian);
mlx_hook(data.mlx_win, 17, 0L, destroy, data.map);
mlx_hook(data.mlx_win, 17, 0L, destroy, &data);
draw_map(data.map, data.mlx_data);
mlx_key_hook(data.mlx_win, key_destroy, data.map);
mlx_key_hook(data.mlx_win, key_destroy, &data);
mlx_put_image_to_window(data.mlx, data.mlx_win, data.mlx_data->img, 0, 0);
mlx_loop(data.mlx);
}

42
src/utils/frees.c Normal file
View file

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* frees.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/25 17:46:01 by tchampio #+# #+# */
/* Updated: 2025/06/25 18:01:29 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/cub3d_consts.h"
#include "../../includes/structs.h"
#include <stdlib.h>
void free_tab(char **tab)
{
int i;
if (!tab)
return ;
i = 0;
while (tab[i])
{
free(tab[i]);
i++;
}
free(tab);
}
void free_map(t_mapdata *map)
{
free_tab(map->map);
free_tab(map->mapflood);
free(map->ea_texture);
free(map->no_texture);
free(map->so_texture);
free(map->we_texture);
free(map->filename);
free(map);
}