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

@@ -6,7 +6,7 @@
/* By: pblagoje <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
game = param;
if (game->fd != -1)
close(game->fd);
if (game->mlx_ptr == NULL)
return ;
if (game->img.ptr)

View File

@@ -6,7 +6,7 @@
/* By: pblagoje <pblagoje@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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_txt_null(&(game->txt));
init_null_mlx_ptr(game);
game->fd = -1;
return (game);
}

View File

@@ -6,7 +6,7 @@
/* By: pblagoje <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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();
tmp = ft_memalloc(size);
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)))
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);
}
@@ -42,7 +42,7 @@ void mb_add(void *addr)
lst = mb_get_lst();
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)
@@ -53,7 +53,7 @@ void mb_free(void *addr)
lst = mb_get_lst();
tmp = ft_lstfind((*lst), addr, mb_comp_addr);
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);
}

View File

@@ -6,13 +6,13 @@
/* By: pblagoje <pblagoje@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
int size_map(t_map *map, int fd)
static int size_map(t_map *map, int fd)
{
char *line;
@@ -41,7 +41,7 @@ int size_map(t_map *map, int fd)
return (EXIT_SUCCESS);
}
int find_map(t_map *map, char *file)
static int find_map(t_map *map, char *file)
{
int fd;
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')
count++;
if (count < TOTAL_ELEMENTS + 1)
mb_free(map->tmp_str);
}
return (fd);
}
void fill_row(char *row, char *line, int width)
static void fill_row(char *row, char *line, int width)
{
int i;
@@ -83,53 +85,48 @@ void fill_row(char *row, char *line, int width)
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 *));
if (!map->content)
mb_exit("Error\nCouldn't allocate memory for map.\n", EXIT_FAILURE);
i = -1;
while (++i < map->size_y)
{
if (i > 0)
{
get_next_line(fd, &map->tmp_str);
ret = get_next_line(fd, &map->tmp_str);
mb_add(map->tmp_str);
}
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);
mb_free(map->tmp_str);
map->tmp_str = 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 fd;
fd = check_elements(game, file);
if (fd == -1)
game->fd = check_elements(game, file);
if (game->fd == -1)
mb_exit("Error\nInvalid .cub file.\n", EXIT_FAILURE);
if (size_map(&(game->map), fd))
{
close(fd);
if (size_map(&(game->map), game->fd))
mb_exit("Error\nInvalid map format.\n", EXIT_FAILURE);
}
close(fd);
fd = find_map(&(game->map), file);
if (fd == -1)
close(game->fd);
game->fd = -1;
game->fd = find_map(&(game->map), file);
if (game->fd == -1)
mb_exit("Error\nCouldn't locate the map.\n", EXIT_FAILURE);
if (fill_map(&(game->map), fd) == EXIT_FAILURE)
{
close(fd);
mb_exit("Error\nCouldn't save the map.\n", EXIT_FAILURE);
}
close(fd);
fill_map(&(game->map), game->fd);
close(game->fd);
game->fd = -1;
return (EXIT_SUCCESS);
}