solved leaks still reachable because gnl wasn't going to the end, and close fd

This commit is contained in:
Hugo LAMY
2022-05-04 22:00:36 +02:00
parent d4e5f298fa
commit c89c107e09
9 changed files with 47 additions and 48 deletions

View File

@@ -81,6 +81,7 @@ RM_OBJS = rm -rf $(D_OBJS)
# flags # flags
CFLAGS = -Wall -Wextra -Werror $(INCLUDES) CFLAGS = -Wall -Wextra -Werror $(INCLUDES)
CFLAGS += -g3 CFLAGS += -g3
#CFLAGS += -fsanitize=address
LFLAGS = -L$(D_LFT) -lft LFLAGS = -L$(D_LFT) -lft
LFLAGS += -L$(D_LMLX) -lm -lmlx -lXext -lX11 LFLAGS += -L$(D_LMLX) -lm -lmlx -lXext -lX11

View File

@@ -6,7 +6,7 @@
/* By: pblagoje <marvin@42.fr> +#+ +:+ +#+ */ /* By: pblagoje <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/04 13:31:38 by pblagoje #+# #+# */ /* Created: 2022/05/04 13:31:38 by pblagoje #+# #+# */
/* Updated: 2022/05/04 13:34:07 by pblagoje ### ########.fr */ /* Updated: 2022/05/04 21:36:13 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -169,7 +169,7 @@ typedef struct s_game
t_rcast rcast; t_rcast rcast;
t_txt txt; t_txt txt;
t_map map; t_map map;
int fd;
int k_hook[MAX_NB_KEY]; int k_hook[MAX_NB_KEY];
} t_game; } t_game;

View File

@@ -1,10 +1,8 @@
R 801 597 NO ./textures/grey.xpm
NO ./texture/grey.xpm SO ./textures/brick.xpm
SO ./texture/brick.xpm WE ./textures/dickbutt.xpm
WE ./texture/wood.xpm EA ./textures/blue.xpm
EA ./texture/blue.xpm
S ./texture/dickbutt.xpm
F 66,66,66 F 66,66,66
C 33,33,33 C 33,33,33
@@ -13,7 +11,7 @@ C 33,33,33
1111111111 1111111111 1111111111 1111111111
100000000111111000001 100000000111111000001
100000000111111000001 100000000111111000001
100000000111111000001111 1000E0000111111000001111
100000000111111000001001 100000000111111000001001
1111111110000000011111100000111111 1111111110000000011111100000111111
1000011000100000000111111111111 1000011000100000000111111111111

View File

@@ -6,7 +6,7 @@
/* By: pblagoje <marvin@42.fr> +#+ +:+ +#+ */ /* By: pblagoje <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/04 13:42:00 by pblagoje #+# #+# */ /* Created: 2022/05/04 13:42:00 by pblagoje #+# #+# */
/* Updated: 2022/05/04 13:43:02 by pblagoje ### ########.fr */ /* Updated: 2022/05/04 21:58:13 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -17,6 +17,8 @@ void destroy_mlx(void *param)
t_game *game; t_game *game;
game = param; game = param;
if (game->fd != -1)
close(game->fd);
if (game->mlx_ptr == NULL) if (game->mlx_ptr == NULL)
return ; return ;
if (game->img.ptr) if (game->img.ptr)

View File

@@ -6,7 +6,7 @@
/* By: pblagoje <pblagoje@student.42.fr> +#+ +:+ +#+ */ /* By: pblagoje <pblagoje@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/27 12:42:20 by pblagoje #+# #+# */ /* Created: 2021/01/27 12:42:20 by pblagoje #+# #+# */
/* Updated: 2022/04/18 17:01:06 by pblagoje ### ########.fr */ /* Updated: 2022/05/04 21:23:28 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@@ -6,7 +6,7 @@
/* By: pblagoje <pblagoje@student.42.fr> +#+ +:+ +#+ */ /* By: pblagoje <pblagoje@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/04 10:35:41 by pblagoje #+# #+# */ /* Created: 2021/02/04 10:35:41 by pblagoje #+# #+# */
/* Updated: 2022/04/18 17:00:41 by pblagoje ### ########.fr */ /* Updated: 2022/05/04 21:07:31 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@@ -6,7 +6,7 @@
/* By: pblagoje <marvin@42.fr> +#+ +:+ +#+ */ /* By: pblagoje <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/04 13:57:22 by pblagoje #+# #+# */ /* Created: 2022/05/04 13:57:22 by pblagoje #+# #+# */
/* Updated: 2022/05/04 13:57:28 by pblagoje ### ########.fr */ /* Updated: 2022/05/04 21:57:53 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -50,5 +50,6 @@ t_game *init_struct(void)
init_map(&(game->map)); init_map(&(game->map));
init_txt_null(&(game->txt)); init_txt_null(&(game->txt));
init_null_mlx_ptr(game); init_null_mlx_ptr(game);
game->fd = -1;
return (game); return (game);
} }

View File

@@ -6,7 +6,7 @@
/* By: pblagoje <marvin@42.fr> +#+ +:+ +#+ */ /* By: pblagoje <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/04 13:56:30 by pblagoje #+# #+# */ /* Created: 2022/05/04 13:56:30 by pblagoje #+# #+# */
/* Updated: 2022/05/04 13:58:58 by pblagoje ### ########.fr */ /* Updated: 2022/05/04 19:47:54 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -30,9 +30,9 @@ void *mb_alloc(size_t size)
lst = mb_get_lst(); lst = mb_get_lst();
tmp = ft_memalloc(size); tmp = ft_memalloc(size);
if (!tmp) if (!tmp)
mb_exit(B_RED"failed create new allocation"RESET"\n", EXIT_FAILURE); mb_exit(B_RED"mb: failed create new allocation"RESET"\n", EXIT_FAILURE);
if (!ft_lstpush_back(lst, ft_lstcreate(tmp))) if (!ft_lstpush_back(lst, ft_lstcreate(tmp)))
mb_exit(B_RED"failed add new element to list"RESET"\n", EXIT_FAILURE); mb_exit(B_RED"mb: failed add new elmnt to list"RESET"\n", EXIT_FAILURE);
return (tmp); return (tmp);
} }
@@ -42,7 +42,7 @@ void mb_add(void *addr)
lst = mb_get_lst(); lst = mb_get_lst();
if (!ft_lstpush_back(lst, ft_lstcreate(addr))) if (!ft_lstpush_back(lst, ft_lstcreate(addr)))
mb_exit(B_RED"failed add new element to list"RESET"\n", EXIT_FAILURE); mb_exit(B_RED"mb: failed add new elmnt to list"RESET"\n", EXIT_FAILURE);
} }
void mb_free(void *addr) void mb_free(void *addr)
@@ -53,7 +53,7 @@ void mb_free(void *addr)
lst = mb_get_lst(); lst = mb_get_lst();
tmp = ft_lstfind((*lst), addr, mb_comp_addr); tmp = ft_lstfind((*lst), addr, mb_comp_addr);
if (!tmp) if (!tmp)
ft_putstr_fd(B_RED"you try to free not allocated address"RESET"\n", 2); ft_putstr_fd(B_RED"mb: try to free not allocated address"RESET"\n", 2);
ft_lsterase(tmp, free); ft_lsterase(tmp, free);
} }

View File

@@ -6,13 +6,13 @@
/* By: pblagoje <pblagoje@student.42.fr> +#+ +:+ +#+ */ /* By: pblagoje <pblagoje@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/15 16:03:50 by pblagoje #+# #+# */ /* Created: 2022/04/15 16:03:50 by pblagoje #+# #+# */
/* Updated: 2022/05/04 15:32:54 by pblagoje ### ########.fr */ /* Updated: 2022/05/04 21:57:24 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "cube3d.h" #include "cube3d.h"
int size_map(t_map *map, int fd) static int size_map(t_map *map, int fd)
{ {
char *line; char *line;
@@ -41,7 +41,7 @@ int size_map(t_map *map, int fd)
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
} }
int find_map(t_map *map, char *file) static int find_map(t_map *map, char *file)
{ {
int fd; int fd;
int count; int count;
@@ -61,11 +61,13 @@ int find_map(t_map *map, char *file)
} }
if (map->tmp_str[0] != '\n' && map->tmp_str[0] != '\0') if (map->tmp_str[0] != '\n' && map->tmp_str[0] != '\0')
count++; count++;
if (count < TOTAL_ELEMENTS + 1)
mb_free(map->tmp_str);
} }
return (fd); return (fd);
} }
void fill_row(char *row, char *line, int width) static void fill_row(char *row, char *line, int width)
{ {
int i; int i;
@@ -83,53 +85,48 @@ void fill_row(char *row, char *line, int width)
row[i] = '\0'; row[i] = '\0';
} }
int fill_map(t_map *map, int fd) static void fill_map(t_map *map, int fd)
{ {
int i; int i;
int ret;
ret = 1;
map->content = (char **)mb_alloc((map->size_y + 1) * sizeof(char *)); map->content = (char **)mb_alloc((map->size_y + 1) * sizeof(char *));
if (!map->content)
mb_exit("Error\nCouldn't allocate memory for map.\n", EXIT_FAILURE);
i = -1; i = -1;
while (++i < map->size_y) while (++i < map->size_y)
{ {
if (i > 0) if (i > 0)
{ {
get_next_line(fd, &map->tmp_str); ret = get_next_line(fd, &map->tmp_str);
mb_add(map->tmp_str); mb_add(map->tmp_str);
} }
map->content[i] = (char *)mb_alloc((map->size_x + 1) * sizeof(char)); map->content[i] = (char *)mb_alloc((map->size_x + 1) * sizeof(char));
if (!map->content[i])
mb_exit("Error\nCouldn't allocate memory for row.\n", EXIT_FAILURE);
fill_row(map->content[i], map->tmp_str, map->size_x); fill_row(map->content[i], map->tmp_str, map->size_x);
mb_free(map->tmp_str); mb_free(map->tmp_str);
map->tmp_str = NULL;
} }
map->content[i] = NULL; map->content[i] = NULL;
return (EXIT_SUCCESS); while (ret)
{
ret = get_next_line(fd, &map->tmp_str);
if (ret)
mb_add(map->tmp_str);
}
} }
int init_parsing(t_game *game, char *file) int init_parsing(t_game *game, char *file)
{ {
int fd; game->fd = check_elements(game, file);
if (game->fd == -1)
fd = check_elements(game, file);
if (fd == -1)
mb_exit("Error\nInvalid .cub file.\n", EXIT_FAILURE); mb_exit("Error\nInvalid .cub file.\n", EXIT_FAILURE);
if (size_map(&(game->map), fd)) if (size_map(&(game->map), game->fd))
{
close(fd);
mb_exit("Error\nInvalid map format.\n", EXIT_FAILURE); mb_exit("Error\nInvalid map format.\n", EXIT_FAILURE);
} close(game->fd);
close(fd); game->fd = -1;
fd = find_map(&(game->map), file); game->fd = find_map(&(game->map), file);
if (fd == -1) if (game->fd == -1)
mb_exit("Error\nCouldn't locate the map.\n", EXIT_FAILURE); mb_exit("Error\nCouldn't locate the map.\n", EXIT_FAILURE);
if (fill_map(&(game->map), fd) == EXIT_FAILURE) fill_map(&(game->map), game->fd);
{ close(game->fd);
close(fd); game->fd = -1;
mb_exit("Error\nCouldn't save the map.\n", EXIT_FAILURE);
}
close(fd);
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
} }