fix: leak

This commit is contained in:
Camille Chauvet 2023-05-05 14:49:04 +00:00
parent 3bda6b4fb1
commit e500e3f440
9 changed files with 76 additions and 43 deletions

View File

@ -1,6 +1,6 @@
SRCS_GAME := main.c game/game.c game/init.c game/manage.c \ SRCS_GAME := main.c game/game.c game/init.c game/manage.c \
game/manage_keys.c game/raycasting.c game/dda.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_MAP := $(addprefix map/, $(SRCS_MAP))
SRCS := ${SRCS_MAP} \ SRCS := ${SRCS_MAP} \

14
main.c
View File

@ -6,27 +6,31 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/26 12:44:55 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 "cube3D.h"
#include "map/map.h"
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
t_map *map; t_map map;
if (argc != 2) if (argc != 2)
{ {
ft_eprintf("No argument"); ft_eprintf("No argument");
return (1); return (1);
} }
map = map_parsing(argv[1]); if (map_parsing(argv[1], &map))
if (!map) {
map_freer(&map);
return (2); return (2);
if (start_game(*map)) }
if (start_game(map))
{ {
return (3); return (3);
} }
map_freer(&map);
return (0); return (0);
} }

16
map/map.c Normal file
View File

@ -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++;
}
}

View File

@ -1,6 +1,7 @@
#include "./parsing_private.h" #include "./parsing_private.h"
#include "map.h" #include "map.h"
#include <stddef.h> #include <stddef.h>
#include <stdlib.h>
static ssize_t get_nb_line(const char *path) static ssize_t get_nb_line(const char *path)
{ {
@ -47,43 +48,54 @@ static char **read_map(const char *path)
return (map); 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 **file_content;
char ***header; char ***header;
char **body;
size_t header_size; size_t header_size;
t_map *map;
if (parsing_meta(path)) if (parsing_meta(path))
return (NULL); return (1);
map = ft_calloc(1, sizeof(t_map)); ft_bzero(map, sizeof(t_map));
if (map == NULL)
return (NULL);
file_content = read_map(path); file_content = read_map(path);
if (file_content == NULL) if (file_content == NULL)
{ {
ft_eprintf("map: file error"); ft_eprintf("map: file error");
return (NULL); return (1);
} }
header = get_header((const char **) file_content, &header_size); header = get_header((const char **) file_content, &header_size);
if (header_is_valid(header, map) == 0) if (header_is_valid(header, map) == 0)
{ {
header_freer(header);
ft_freer_tab_ultimate(1, file_content); ft_freer_tab_ultimate(1, file_content);
ft_freer_ultimate(1, map); return (1);
return (NULL);
} }
//ici aussi header; header_freer(header);
body = get_body((const char **) file_content, header_size); map->map = get_body((const char **) file_content, header_size);
ft_freer_tab_ultimate(1, file_content); ft_freer_tab_ultimate(1, file_content);
if (body_is_valid((const char **) body, map) == 0) if (body_is_valid(map) == 0)
{ return (1);
ft_freer_ultimate(1, map);
// faudra free
return (NULL);
}
map->map = body;
map->direction = map->map[(size_t) map->ply_y][(size_t) map->ply_x]; 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'; map->map[(size_t) map->ply_y][(size_t) map->ply_x] = '0';
return (map); return (0);
} }

View File

@ -2,6 +2,6 @@
# define PARSING_H # define PARSING_H
# include "map.h" # include "map.h"
t_map *map_parsing(const char *path); int map_parsing(const char *path, t_map *map);
#endif #endif

View File

@ -1,5 +1,4 @@
#include "parsing_private.h" #include "parsing_private.h"
#include <stddef.h>
char **get_body(const char **file_content, size_t header_size) 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; map->size_y = y;
} }
int body_is_valid(const char **body, t_map *map) int body_is_valid(t_map *map)
{ {
int error; int error;
if (body == NULL) if (map->map == NULL)
return (0); 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) if (error == 1)
{ {
ft_eprintf("map: spawn position: multiple definition"); 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"); ft_eprintf("map: spawn position: not define");
return (0); return (0);
} }
error = map_surround(body); error = map_surround((const char **)map->map);
if (error) if (error)
{ {
ft_eprintf("map: not surrounded"); ft_eprintf("map: not surrounded");
return (0); return (0);
} }
error = map_is_in_one_part(body); error = map_is_in_one_part((const char **)map->map);
if (error) if (error)
{ {
ft_eprintf("map: splitted"); ft_eprintf("map: splitted");
return (0); return (0);
} }
if (body_contain_normal_char(body) == 0) if (body_contain_normal_char((const char **)map->map) == 0)
return (0); return (0);
get_size(body, map); get_size((const char **)map->map, map);
return (1); return (1);
} }

View File

@ -25,20 +25,20 @@ static int is_header(const char *line)
if (list == NULL) if (list == NULL)
{ {
ft_eprintf("malloc failed"); ft_eprintf("malloc failed");
ft_freer_ultimate(1, list); ft_freer_tab_ultimate(1, list);
return (0); return (0);
} }
if (ft_tablen((const void **) list) != 2) if (ft_tablen((const void **) list) != 2)
{ {
ft_freer_ultimate(1, list); ft_freer_tab_ultimate(1, list);
return (0); return (0);
} }
if (get_token(list[0]) == 0) if (get_token(list[0]) == 0)
{ {
ft_freer_ultimate(1, list); ft_freer_tab_ultimate(1, list);
return (0); return (0);
} }
ft_freer_ultimate(1, list); ft_freer_tab_ultimate(1, list);
return (1); return (1);
} }

View File

@ -1,5 +1,4 @@
#include "./parsing_private.h" #include "./parsing_private.h"
#include <stddef.h>
static int set_texture(t_map *map, int token, const char *key, char *value) 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); ft_eprintf("redefinition of %s", key);
return (1); 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); return (0);
} }
@ -61,6 +62,7 @@ static int set_color(long long *color, const char *key, char *value)
return (1); return (1);
} }
*color = get_color((const char **) tab); *color = get_color((const char **) tab);
ft_freer_tab_ultimate(1, tab);
return (*color == -1); return (*color == -1);
} }

View File

@ -9,5 +9,5 @@ int get_token(const char *str);
char ***get_header(const char **file_content, size_t *header_size); char ***get_header(const char **file_content, size_t *header_size);
int header_is_valid(char ***header, t_map *map); int header_is_valid(char ***header, t_map *map);
char **get_body(const char **file_content, size_t header_size); 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 #endif