From e500e3f440f454d0104401a144b36a14e069eede Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Fri, 5 May 2023 14:49:04 +0000 Subject: [PATCH] fix: leak --- Makefile | 2 +- main.c | 14 +++++++---- map/map.c | 16 +++++++++++++ map/parsing.c | 54 ++++++++++++++++++++++++++----------------- map/parsing.h | 2 +- map/parsing_body.c | 15 ++++++------ map/parsing_header.c | 8 +++---- map/parsing_header2.c | 6 +++-- map/parsing_private.h | 2 +- 9 files changed, 76 insertions(+), 43 deletions(-) create mode 100644 map/map.c diff --git a/Makefile b/Makefile index 77ec693..4d16625 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ SRCS_GAME := main.c game/game.c game/init.c game/manage.c \ game/manage_keys.c game/raycasting.c game/dda.c -SRCS_MAP := parsing.c parsing_header.c parsing_header2.c parsing_meta.c parsing_body.c +SRCS_MAP := parsing.c parsing_header.c parsing_header2.c parsing_meta.c parsing_body.c map.c SRCS_MAP := $(addprefix map/, $(SRCS_MAP)) SRCS := ${SRCS_MAP} \ diff --git a/main.c b/main.c index 830b8e5..f8b8617 100644 --- a/main.c +++ b/main.c @@ -6,27 +6,31 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/26 12:44:55 by erey-bet #+# #+# */ -/* Updated: 2023/05/05 11:47:28 by cchauvet ### ########.fr */ +/* Updated: 2023/05/05 14:48:45 by cchauvet ### ########.fr */ /* */ /* ************************************************************************** */ #include "cube3D.h" +#include "map/map.h" int main(int argc, char **argv) { - t_map *map; + t_map map; if (argc != 2) { ft_eprintf("No argument"); return (1); } - map = map_parsing(argv[1]); - if (!map) + if (map_parsing(argv[1], &map)) + { + map_freer(&map); return (2); - if (start_game(*map)) + } + if (start_game(map)) { return (3); } + map_freer(&map); return (0); } diff --git a/map/map.c b/map/map.c new file mode 100644 index 0000000..b10e036 --- /dev/null +++ b/map/map.c @@ -0,0 +1,16 @@ +#include "map.h" +#include "./parsing_private.h" + +void map_freer(t_map *map) +{ + size_t i; + + ft_freer_tab_ultimate(1, map->map); + i = 0; + while (i < 4) + { + if (map->img_path[i] != NULL) + free(map->img_path[i]); + i++; + } +} diff --git a/map/parsing.c b/map/parsing.c index 5d5b889..d9af107 100644 --- a/map/parsing.c +++ b/map/parsing.c @@ -1,6 +1,7 @@ #include "./parsing_private.h" #include "map.h" #include +#include static ssize_t get_nb_line(const char *path) { @@ -47,43 +48,54 @@ static char **read_map(const char *path) return (map); } -t_map *map_parsing(const char *path) +void header_freer(char ***header) +{ + size_t i; + size_t j; + + i = 0; + while (header[i] != NULL) + { + j = 0; + while (header[i][j] != NULL) + { + free(header[i][j]); + j++; + } + free(header[i]); + i++; + } + free(header); +} + +int map_parsing(const char *path, t_map *map) { char **file_content; char ***header; - char **body; size_t header_size; - t_map *map; if (parsing_meta(path)) - return (NULL); - map = ft_calloc(1, sizeof(t_map)); - if (map == NULL) - return (NULL); + return (1); + ft_bzero(map, sizeof(t_map)); file_content = read_map(path); if (file_content == NULL) { ft_eprintf("map: file error"); - return (NULL); + return (1); } header = get_header((const char **) file_content, &header_size); if (header_is_valid(header, map) == 0) { + header_freer(header); ft_freer_tab_ultimate(1, file_content); - ft_freer_ultimate(1, map); - return (NULL); + return (1); } - //ici aussi header; - body = get_body((const char **) file_content, header_size); - ft_freer_tab_ultimate(1, file_content); - if (body_is_valid((const char **) body, map) == 0) - { - ft_freer_ultimate(1, map); - // faudra free - return (NULL); - } - map->map = body; + header_freer(header); + map->map = get_body((const char **) file_content, header_size); + ft_freer_tab_ultimate(1, file_content); + if (body_is_valid(map) == 0) + return (1); map->direction = map->map[(size_t) map->ply_y][(size_t) map->ply_x]; map->map[(size_t) map->ply_y][(size_t) map->ply_x] = '0'; - return (map); + return (0); } diff --git a/map/parsing.h b/map/parsing.h index 148c31f..20d7528 100644 --- a/map/parsing.h +++ b/map/parsing.h @@ -2,6 +2,6 @@ # define PARSING_H # include "map.h" -t_map *map_parsing(const char *path); +int map_parsing(const char *path, t_map *map); #endif diff --git a/map/parsing_body.c b/map/parsing_body.c index ce88387..f568507 100644 --- a/map/parsing_body.c +++ b/map/parsing_body.c @@ -1,5 +1,4 @@ #include "parsing_private.h" -#include char **get_body(const char **file_content, size_t header_size) { @@ -141,13 +140,13 @@ void get_size(const char **body, t_map *map) map->size_y = y; } -int body_is_valid(const char **body, t_map *map) +int body_is_valid(t_map *map) { int error; - if (body == NULL) + if (map->map == NULL) return (0); - error = get_spawn_position(body, &map->ply_x, &map->ply_y); + error = get_spawn_position((const char **) map->map, &map->ply_x, &map->ply_y); if (error == 1) { ft_eprintf("map: spawn position: multiple definition"); @@ -158,20 +157,20 @@ int body_is_valid(const char **body, t_map *map) ft_eprintf("map: spawn position: not define"); return (0); } - error = map_surround(body); + error = map_surround((const char **)map->map); if (error) { ft_eprintf("map: not surrounded"); return (0); } - error = map_is_in_one_part(body); + error = map_is_in_one_part((const char **)map->map); if (error) { ft_eprintf("map: splitted"); return (0); } - if (body_contain_normal_char(body) == 0) + if (body_contain_normal_char((const char **)map->map) == 0) return (0); - get_size(body, map); + get_size((const char **)map->map, map); return (1); } diff --git a/map/parsing_header.c b/map/parsing_header.c index 3c8750b..3989c94 100644 --- a/map/parsing_header.c +++ b/map/parsing_header.c @@ -25,20 +25,20 @@ static int is_header(const char *line) if (list == NULL) { ft_eprintf("malloc failed"); - ft_freer_ultimate(1, list); + ft_freer_tab_ultimate(1, list); return (0); } if (ft_tablen((const void **) list) != 2) { - ft_freer_ultimate(1, list); + ft_freer_tab_ultimate(1, list); return (0); } if (get_token(list[0]) == 0) { - ft_freer_ultimate(1, list); + ft_freer_tab_ultimate(1, list); return (0); } - ft_freer_ultimate(1, list); + ft_freer_tab_ultimate(1, list); return (1); } diff --git a/map/parsing_header2.c b/map/parsing_header2.c index 8391b91..e543a17 100644 --- a/map/parsing_header2.c +++ b/map/parsing_header2.c @@ -1,5 +1,4 @@ #include "./parsing_private.h" -#include static int set_texture(t_map *map, int token, const char *key, char *value) { @@ -8,7 +7,9 @@ static int set_texture(t_map *map, int token, const char *key, char *value) ft_eprintf("redefinition of %s", key); return (1); } - map->img[token - 1] = value; + map->img_path[token - 1] = ft_strdup(value); + if (map->img_path[token - 1] == NULL) + return (1); return (0); } @@ -61,6 +62,7 @@ static int set_color(long long *color, const char *key, char *value) return (1); } *color = get_color((const char **) tab); + ft_freer_tab_ultimate(1, tab); return (*color == -1); } diff --git a/map/parsing_private.h b/map/parsing_private.h index e2b7187..f2ddbfa 100644 --- a/map/parsing_private.h +++ b/map/parsing_private.h @@ -9,5 +9,5 @@ int get_token(const char *str); char ***get_header(const char **file_content, size_t *header_size); int header_is_valid(char ***header, t_map *map); char **get_body(const char **file_content, size_t header_size); -int body_is_valid(const char **body, t_map *map); +int body_is_valid(t_map *map); #endif