fix differents leaks when error

This commit is contained in:
kinou-p 2022-06-15 16:21:00 +02:00
parent ff64788b94
commit cc9b8f0d37
6 changed files with 72 additions and 49 deletions

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/13 16:30:59 by apommier #+# #+# */ /* Created: 2022/02/13 16:30:59 by apommier #+# #+# */
/* Updated: 2022/06/15 15:29:25 by apommier ### ########.fr */ /* Updated: 2022/06/15 15:51:37 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -86,6 +86,8 @@ typedef struct player_position
typedef struct need_to_be_free typedef struct need_to_be_free
{ {
char **tab; char **tab;
char **tab_two;
unsigned char *str;
int fd; int fd;
} to_free; } to_free;

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/06 16:10:11 by apommier #+# #+# */ /* Created: 2022/05/06 16:10:11 by apommier #+# #+# */
/* Updated: 2022/06/15 15:30:23 by apommier ### ########.fr */ /* Updated: 2022/06/15 16:12:02 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -85,10 +85,11 @@ unsigned char *get_texture(char type, char *path, t_data *img)//change in list
close(fd); close(fd);
img->to_be_free.fd = -1; img->to_be_free.fd = -1;
if (count != 12291) if (count != 12291)
ft_exit("Error\n Bad texture file\n", img); ft_exit("Error\nBad texture file\n", img);
ret = ft_calloc(sizeof(char), count); ret = ft_calloc(sizeof(char), count);
if (!ret) if (!ret)
quit_game(img); quit_game(img);
img->to_be_free.str = ret;
fd = open(path, O_RDONLY); fd = open(path, O_RDONLY);
if (fd == -1) if (fd == -1)
ft_exit("Error\nBad texture file", img); ft_exit("Error\nBad texture file", img);
@ -104,7 +105,6 @@ unsigned char *get_texture(char type, char *path, t_data *img)//change in list
{ {
free(swap); free(swap);
swap = get_next_line(fd); swap = get_next_line(fd);
} }
if (swap) if (swap)
swap[ft_strlen(swap) - 1] = 0; swap[ft_strlen(swap) - 1] = 0;
@ -112,14 +112,14 @@ unsigned char *get_texture(char type, char *path, t_data *img)//change in list
ret[count] = (unsigned char)ft_atoi(swap); ret[count] = (unsigned char)ft_atoi(swap);
else if (swap) else if (swap)
{ {
//printf("isnbr= %d swap= %s ft_atoi(swap)= ---%ld--- strlen= %ld\n",is_nbr(swap) ,swap , ft_atoi(swap), ft_strlen(swap) ); free(swap);
free(ret); ft_exit("Error\nBad texture file2\n", img);
ft_exit("Error\nBad texture file\n", img);
} }
count++; count++;
} }
close(fd); close(fd);
img->to_be_free.fd = -1; img->to_be_free.fd = -1;
put_texture_in_struct(type, ret, img); put_texture_in_struct(type, ret, img);
img->to_be_free.str = 0;
return (ret); return (ret);
} }

View File

@ -6,18 +6,19 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/13 16:42:55 by apommier #+# #+# */ /* Created: 2022/02/13 16:42:55 by apommier #+# #+# */
/* Updated: 2022/06/15 15:25:12 by apommier ### ########.fr */ /* Updated: 2022/06/15 15:52:45 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../includes/Cub3D.h" #include "../includes/Cub3D.h"
void set_map(t_data *img) void set_map_player(t_data *img)
{ {
img->map.texture.north = 0; img->map.texture.north = 0;
img->map.texture.east = 0; img->map.texture.east = 0;
img->map.texture.south = 0; img->map.texture.south = 0;
img->map.texture.west = 0; img->map.texture.west = 0;
img->map.simple_map = 0;
img->player.x = 0; img->player.x = 0;
img->player.y = 0; img->player.y = 0;
img->player.angle = 0; img->player.angle = 0;
@ -27,8 +28,12 @@ void set_map(t_data *img)
img->player.side = 0; img->player.side = 0;
img->player.angle_side = 0; img->player.angle_side = 0;
img->player.shift = 0; img->player.shift = 0;
img->to_be_free.str = 0;
img->to_be_free.tab = NULL; img->to_be_free.tab = NULL;
img->to_be_free.tab_two = NULL;
img->to_be_free.fd = -1; img->to_be_free.fd = -1;
img->mlx = 0;
img->mlx_win = 0;
} }
int key_pressed(int type, t_data *img) int key_pressed(int type, t_data *img)
@ -84,7 +89,7 @@ int main(int argc, char **argv)
if (argc != 2) if (argc != 2)
ft_error("Error\nBad number of arguments, only need a map\n"); ft_error("Error\nBad number of arguments, only need a map\n");
set_map(&img); set_map_player(&img);
img.player.x = 0; img.player.x = 0;
img.player.y = 0; img.player.y = 0;
if (check_map(argv, &img)) if (check_map(argv, &img))

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/11 01:00:17 by apommier #+# #+# */ /* Created: 2022/06/11 01:00:17 by apommier #+# #+# */
/* Updated: 2022/06/15 15:27:14 by apommier ### ########.fr */ /* Updated: 2022/06/15 15:49:19 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -108,7 +108,7 @@ void check_value(char **tab, t_data *img)
while (tab[i][j]) while (tab[i][j])
{ {
if (!ft_isdigit(tab[i][j])) if (!ft_isdigit(tab[i][j]))
ft_exit("Error\nBad syntax in map file (RGB)2\n", img); ft_exit("Error\nRGB parameters need to be only digits\n", img);
j++; j++;
} }
i++; i++;
@ -118,11 +118,11 @@ void check_value(char **tab, t_data *img)
void set_color_utils(char **tab, color *rgb, t_data *img) void set_color_utils(char **tab, color *rgb, t_data *img)
{ {
if (ft_strlen(tab[0]) > 3 || !(ft_atoi(tab[0]) >= 0 && ft_atoi(tab[0]) <= 255)) if (ft_strlen(tab[0]) > 3 || !(ft_atoi(tab[0]) >= 0 && ft_atoi(tab[0]) <= 255))
ft_exit("Error\nBad syntax in map file (RGB)\n", img); ft_exit("Error\nBad parameter in map file identifier(RGB)\n", img);
if (ft_strlen(tab[0]) > 3 || !(ft_atoi(tab[1]) >= 0 && ft_atoi(tab[1]) <= 255)) if (ft_strlen(tab[0]) > 3 || !(ft_atoi(tab[1]) >= 0 && ft_atoi(tab[1]) <= 255))
ft_exit("Error\nBad syntax in map file (RGB)\n", img); ft_exit("Error\nBad parameter in map file identifier(RGB)\n", img);
if (ft_strlen(tab[0]) > 3 || !(ft_atoi(tab[2]) >= 0 && ft_atoi(tab[2]) <= 255)) if (ft_strlen(tab[0]) > 3 || !(ft_atoi(tab[2]) >= 0 && ft_atoi(tab[2]) <= 255))
ft_exit("Error\nBad syntax in map file (RGB)\n", img); ft_exit("Error\nBad parameter in map file identifier(RGB)\n", img);
check_value(tab, img); check_value(tab, img);
rgb->r = (unsigned char)ft_atoi(tab[0]); rgb->r = (unsigned char)ft_atoi(tab[0]);
rgb->g = (unsigned char)ft_atoi(tab[1]); rgb->g = (unsigned char)ft_atoi(tab[1]);
@ -138,12 +138,19 @@ void set_color(char *str, t_data *img)
int index; int index;
char **tab; char **tab;
index = -1;
while (str[++index])
{
if (str[index] == ',' && str[index + 1] == ',')
ft_exit("Error\nBad syntax for RGB identifier\n", img);
}
//printf("str=%s \n", str); //printf("str=%s \n", str);
c = next_space(str, 0); c = next_space(str, 0);
index = next_space_index(str + 1, 0); index = next_space_index(str + 1, 0);
tab = ft_split(str + index + 1, ','); tab = ft_split(str + index + 1, ',');
if (!tab) if (!tab)
quit_game(img); quit_game(img);
img->to_be_free.tab_two = tab;
//if (double_size(tab) != 3) //if (double_size(tab) != 3)
// ft_exit("Error\nBad syntax in map file (RGB) 1\n"); // ft_exit("Error\nBad syntax in map file (RGB) 1\n");
//print_double_fd(tab, 1); //print_double_fd(tab, 1);
@ -152,6 +159,7 @@ void set_color(char *str, t_data *img)
if (c == 'C') if (c == 'C')
set_color_utils(tab, &img->map.sky, img); set_color_utils(tab, &img->map.sky, img);
free_double(tab); free_double(tab);
img->to_be_free.tab_two = 0;
} }
int check_texture_color(char **tab, t_data *img) int check_texture_color(char **tab, t_data *img)
@ -168,7 +176,7 @@ int check_texture_color(char **tab, t_data *img)
while (next_space(tab[next], 0) != '1' && tab[next]) while (next_space(tab[next], 0) != '1' && tab[next])
next++; next++;
if (!tab[next]) if (!tab[next])
ft_exit("Error\nBad syntax in map file\n", img); ft_exit("Error\nBad syntax in map file (identifier)\n", img);
while (index < next) while (index < next)
{ {
if (next_space(tab[index], 0) == 'N' || next_space(tab[index], 0) == 'S' || next_space(tab[index], 0) == 'W' || next_space(tab[index], 0) == 'E') if (next_space(tab[index], 0) == 'N' || next_space(tab[index], 0) == 'S' || next_space(tab[index], 0) == 'W' || next_space(tab[index], 0) == 'E')
@ -179,7 +187,7 @@ int check_texture_color(char **tab, t_data *img)
} }
else if (next_space(tab[index], 0)) else if (next_space(tab[index], 0))
ft_exit("Error\nBad syntax in map file\n", img); ft_exit("Error\nBad syntax in map file (identifier)\n", img);
index++; index++;
} }
return (next); return (next);

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/10 17:54:25 by sadjigui #+# #+# */ /* Created: 2022/05/10 17:54:25 by sadjigui #+# #+# */
/* Updated: 2022/06/15 15:26:12 by apommier ### ########.fr */ /* Updated: 2022/06/15 15:54:55 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -37,13 +37,13 @@ void verifie_texture_color(t_data *img)
void error_msg(t_data *img) void error_msg(t_data *img)
{ {
if (img->map.error == 1) if (img->map.error == 1)
ft_exit("Error: Map isn't closed\n", img); ft_exit("Error\nMap isn't closed\n", img);
if (img->map.error == 2) if (img->map.error == 2)
ft_exit("Error: Missing player\n", img); ft_exit("Error\nMissing player\n", img);
if (img->map.error == 3) if (img->map.error == 3)
ft_exit("Error: Too many players\n", img); ft_exit("Error\nToo many players\n", img);
if (img->map.error == -1) if (img->map.error == -1)
ft_exit("Error: Bad character in map\n", img); ft_exit("Error\nBad character in map\n", img);
} }
void size_line(char *str, t_data *img) void size_line(char *str, t_data *img)
@ -289,7 +289,7 @@ char **isafile(char **av, t_data *img)
str = NULL; str = NULL;
tmp = NULL; tmp = NULL;
if (fd == - 1) if (fd == - 1)
ft_exit("Error: File doesn't exist\n", img); ft_exit("Error\nFile doesn't exist\n", img);
while ((line = get_next_line(fd)) != NULL) while ((line = get_next_line(fd)) != NULL)
{ {
if (line[0] != '\n' && img->err == 0) if (line[0] != '\n' && img->err == 0)
@ -337,7 +337,7 @@ int check_map(char **av, t_data *img)
if (reverse_comp(av[1], ".cub") || (ft_strlen(av[1]) == ft_strlen(".cub"))) if (reverse_comp(av[1], ".cub") || (ft_strlen(av[1]) == ft_strlen(".cub")))
{ {
ft_exit("Error: Not a valid file \".cub\"\n", img); ft_exit("Error\nNot a valid file \".cub\"\n", img);
return (1); return (1);
} }
isafile(av, img); isafile(av, img);

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/13 23:37:02 by apommier #+# #+# */ /* Created: 2022/02/13 23:37:02 by apommier #+# #+# */
/* Updated: 2022/06/15 15:29:19 by apommier ### ########.fr */ /* Updated: 2022/06/15 15:53:35 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,14 +26,22 @@ void free_texture(t_data *img)
int quit_game(t_data *img) int quit_game(t_data *img)
{ {
if (img->mlx)
{
if (img->mlx_win)
mlx_destroy_window(img->mlx, img->mlx_win); mlx_destroy_window(img->mlx, img->mlx_win);
mlx_destroy_display(img->mlx); mlx_destroy_display(img->mlx);
if (img->mlx)
free(img->mlx); free(img->mlx);
}
free_texture(img); free_texture(img);
if (img->map.simple_map)
free(img->map.simple_map); free(img->map.simple_map);
if (img->to_be_free.tab) if (img->to_be_free.tab)
free_double(img->to_be_free.tab); free_double(img->to_be_free.tab);
if (img->to_be_free.tab_two)
free_double(img->to_be_free.tab_two);
if (img->to_be_free.str)
free(img->to_be_free.str);
if (img->to_be_free.fd != -1) if (img->to_be_free.fd != -1)
close(img->to_be_free.fd); close(img->to_be_free.fd);