fix differents leak when error

This commit is contained in:
kinou-p 2022-06-15 17:26:19 +02:00
parent f71b846372
commit 72ab74692c
5 changed files with 18 additions and 41 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:51:37 by apommier ### ########.fr */ /* Updated: 2022/06/15 17:02:52 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -88,6 +88,7 @@ typedef struct need_to_be_free
char **tab; char **tab;
char **tab_two; char **tab_two;
unsigned char *str; unsigned char *str;
char *string;
int fd; int fd;
} to_free; } to_free;

View File

@ -14,12 +14,15 @@
char *ft_free(char *save, int *end) char *ft_free(char *save, int *end)
{ {
if (!*end) if (!end || !*end)
{ {
if (save)
free(save); free(save);
if (end)
free(end); free(end);
return (0); return (0);
} }
if (end)
free(end); free(end);
return (save); return (save);
} }
@ -81,7 +84,10 @@ char *get_next_line(int fd)
line = 0; line = 0;
if (fd < 0) if (fd < 0)
{
ft_free(save, 0);
return (0); return (0);
}
end = malloc(sizeof(int *)); end = malloc(sizeof(int *));
*end = 1; *end = 1;
if (save == NULL) if (save == NULL)

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 16:12:02 by apommier ### ########.fr */ /* Updated: 2022/06/15 17:25:21 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -85,7 +85,7 @@ 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\nBad texture file\n", img); ft_exit("Error\nBad texture file (too much line)\n", img);
ret = ft_calloc(sizeof(char), count); ret = ft_calloc(sizeof(char), count);
if (!ret) if (!ret)
quit_game(img); quit_game(img);
@ -100,7 +100,6 @@ unsigned char *get_texture(char type, char *path, t_data *img)//change in list
if (swap) if (swap)
free(swap); free(swap);
swap = get_next_line(fd); swap = get_next_line(fd);
//printf("str = -%s-\n", swap);
if (!count) if (!count)
{ {
free(swap); free(swap);
@ -112,8 +111,9 @@ 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)
{ {
get_next_line(-1);
free(swap); free(swap);
ft_exit("Error\nBad texture file2\n", img); ft_exit("Error\nBad texture file (not numbers or to high)\n", img);
} }
count++; count++;
} }

View File

@ -6,7 +6,7 @@
/* 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:52:45 by apommier ### ########.fr */ /* Updated: 2022/06/15 17:16:33 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -108,6 +108,5 @@ int main(int argc, char **argv)
mlx_hook(img.mlx_win, 3, 1L << 1, key_released, &img); mlx_hook(img.mlx_win, 3, 1L << 1, key_released, &img);
mlx_loop_hook(img.mlx, loop, &img); mlx_loop_hook(img.mlx, loop, &img);
mlx_hook(img.mlx_win, 17, 0L, &quit_game, &img); mlx_hook(img.mlx_win, 17, 0L, &quit_game, &img);
mlx_loop(img.mlx); mlx_loop(img.mlx);
} }

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:49:19 by apommier ### ########.fr */ /* Updated: 2022/06/15 17:24:06 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,13 +26,6 @@ int next_space_index(char *str, int i)
return (i); return (i);
} }
/*char next_line_space(char **tab, int i)
{
while (!next_space(tab[i], 0))
i++;
return (tab[i]);
}*/
char *transform_map(char **double_map, t_data *img) char *transform_map(char **double_map, t_data *img)
{ {
char *map; char *map;
@ -42,9 +35,7 @@ char *transform_map(char **double_map, t_data *img)
i = -1; i = -1;
index = 0; index = 0;
//print_double_fd(double_map, 1);
img->map.size = img->map.x * img->map.y; img->map.size = img->map.x * img->map.y;
//printf("x= %d y= %d\n", img->map.x, img->map.y);
map = ft_calloc(sizeof(char), img->map.size + 1); map = ft_calloc(sizeof(char), img->map.size + 1);
if (!map) if (!map)
quit_game(img); quit_game(img);
@ -53,17 +44,11 @@ char *transform_map(char **double_map, t_data *img)
j = -1; j = -1;
while (double_map[i][++j]) while (double_map[i][++j])
{ {
//printf("INDEX= %d\n", i * img->map.x + j);
//if ()
map[i * img->map.x + j] = double_map[i][j]; map[i * img->map.x + j] = double_map[i][j];
//map[index] = double_map[i][j];
//printf("index= %d\n", index);
index++; index++;
} }
} }
img->map.simple_map = map; img->map.simple_map = map;
//printf("map = -%s-\n", map);
return (0); return (0);
} }
@ -92,7 +77,6 @@ void set_texture_file(char *str, t_data *img, char c)
if (str[index + 1] != 'A' || str[index + 2] != ' ') if (str[index + 1] != 'A' || str[index + 2] != ' ')
ft_exit("Error\nBad syntax in map file\n", img); ft_exit("Error\nBad syntax in map file\n", img);
} }
//printf("path= -%s-\n", str + index + 3);
get_texture(c, str + index + 3, img); get_texture(c, str + index + 3, img);
} }
@ -128,8 +112,6 @@ void set_color_utils(char **tab, color *rgb, t_data *img)
rgb->g = (unsigned char)ft_atoi(tab[1]); rgb->g = (unsigned char)ft_atoi(tab[1]);
rgb->b = (unsigned char)ft_atoi(tab[2]); rgb->b = (unsigned char)ft_atoi(tab[2]);
rgb->set = 1; rgb->set = 1;
//printf("---%d\n", img->g);
} }
void set_color(char *str, t_data *img) void set_color(char *str, t_data *img)
@ -144,16 +126,12 @@ void set_color(char *str, t_data *img)
if (str[index] == ',' && str[index + 1] == ',') if (str[index] == ',' && str[index + 1] == ',')
ft_exit("Error\nBad syntax for RGB identifier\n", img); ft_exit("Error\nBad syntax for RGB identifier\n", img);
} }
//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; img->to_be_free.tab_two = tab;
//if (double_size(tab) != 3)
// ft_exit("Error\nBad syntax in map file (RGB) 1\n");
//print_double_fd(tab, 1);
if (c == 'F') if (c == 'F')
set_color_utils(tab, &img->map.floor, img); set_color_utils(tab, &img->map.floor, img);
if (c == 'C') if (c == 'C')
@ -166,13 +144,9 @@ int check_texture_color(char **tab, t_data *img)
{ {
int next; int next;
int index; int index;
//char line;
index = 0; index = 0;
next = 0; next = 0;
//int i = 0;
//while (tab[i])
// printf("%s\n", tab[i++]);
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])
@ -182,10 +156,7 @@ int check_texture_color(char **tab, t_data *img)
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')
set_texture_file(tab[index], img, next_space(tab[index], 0)); set_texture_file(tab[index], img, next_space(tab[index], 0));
else if (next_space(tab[index], 0) == 'F' || next_space(tab[index], 0) == 'C') else if (next_space(tab[index], 0) == 'F' || next_space(tab[index], 0) == 'C')
{
set_color(tab[index], img); set_color(tab[index], img);
}
else if (next_space(tab[index], 0)) else if (next_space(tab[index], 0))
ft_exit("Error\nBad syntax in map file (identifier)\n", img); ft_exit("Error\nBad syntax in map file (identifier)\n", img);
index++; index++;