From 5a7424600da8d12503c0a1616204b081dc0f48f5 Mon Sep 17 00:00:00 2001 From: H3XploR Date: Tue, 25 Feb 2025 19:09:14 +0100 Subject: [PATCH] regler leak dans exit --- array.c | 4 ++-- config.c | 37 ++++++++++++++++++++++++++--- ft_realloc.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ libft | 2 +- miniRT.h | 6 ++++- peripherique.c | 5 +++- tags | 7 +++++- 7 files changed, 116 insertions(+), 9 deletions(-) create mode 100644 ft_realloc.c diff --git a/array.c b/array.c index 7db6111..d2ff73d 100644 --- a/array.c +++ b/array.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_arraylen.c :+: :+: :+: */ +/* array.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/17 21:10:10 by yantoine #+# #+# */ -/* Updated: 2025/02/17 21:21:30 by yantoine ### ########.fr */ +/* Updated: 2025/02/25 18:46:02 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/config.c b/config.c index 1eb4b3d..b929c59 100644 --- a/config.c +++ b/config.c @@ -6,7 +6,7 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/14 01:41:17 by yantoine #+# #+# */ -/* Updated: 2025/02/25 17:52:55 by yantoine ### ########.fr */ +/* Updated: 2025/02/25 19:05:30 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,10 +34,39 @@ static inline t_scene parsing_line(char *line, t_scene scene) return (scene); } +static char **get_all_file(int fd) +{ + char *join; + char *line; + char *data; + char **splited; + int first; + + first = 1; + line = NULL; + data = ft_calloc(1, 1); + while (1) + { + if (!first) + data = join; + line = get_next_line(fd); + if (!line) + break ; + join = ft_strjoin(data, line); + free(line); + free(data); + first = 0; + } + splited = ft_split(join, '\n'); + free(data); + return (splited); +} + // ----- Parsing du fichier de configuration ----- t_scene load_config(const char *filename) { int fd; + int i; char *line; t_scene scene; @@ -49,9 +78,11 @@ t_scene load_config(const char *filename) return (scene); } scene.fd_if_exit = fd; - while (1) + scene.all_file = get_all_file(fd); + i = -1; + while (scene.all_file[++i]) { - line = get_next_line(fd); + line = scene.all_file[i]; if (!line) break ; scene = parsing_line(line, scene); diff --git a/ft_realloc.c b/ft_realloc.c new file mode 100644 index 0000000..ab9a5eb --- /dev/null +++ b/ft_realloc.c @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_realloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yantoine +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/25 17:58:34 by yantoine #+# #+# */ +/* Updated: 2025/02/25 18:06:12 by yantoine ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "miniRT.h" + +static void *allocate_memory(size_t new_size) +{ + void *new_ptr; + + new_ptr = malloc(new_size); + if (!new_ptr) + return (NULL); + return (new_ptr); +} + +static void copy_memory(void *dst, void *src, size_t size) +{ + size_t i; + + i = 0; + while (i < size) + { + ((char *)dst)[i] = ((char *)src)[i]; + i++; + } +} + +static size_t get_copy_size(size_t old_size, size_t new_size) +{ + if (old_size < new_size) + return (old_size); + return (new_size); +} + +void *ft_realloc(void *ptr, size_t old_size, size_t new_size) +{ + void *new_ptr; + size_t copy_size; + + if (new_size == 0) + { + free(ptr); + return (NULL); + } + new_ptr = allocate_memory(new_size); + if (!new_ptr) + return (NULL); + if (ptr) + { + copy_size = get_copy_size(old_size, new_size); + copy_memory(new_ptr, ptr, copy_size); + free(ptr); + } + return (new_ptr); +} diff --git a/libft b/libft index 2b3e413..17b5a6f 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 2b3e413e157169f4cb5abf69f9e0b1aa05636121 +Subproject commit 17b5a6fdc0228f89d8af799cec7ddc41313d5465 diff --git a/miniRT.h b/miniRT.h index e4ebc7a..a9eff99 100644 --- a/miniRT.h +++ b/miniRT.h @@ -6,7 +6,7 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/13 20:02:36 by yantoine #+# #+# */ -/* Updated: 2025/02/25 00:34:07 by yantoine ### ########.fr */ +/* Updated: 2025/02/25 18:35:45 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -132,6 +132,7 @@ typedef struct s_scene int num_ambient; char *line_if_exit; char **token_if_exit; + char **all_file; int fd_if_exit; } t_scene; @@ -286,4 +287,7 @@ void print_ambient(t_ambient amb); void print_camera(t_camera cam); void print_scene(t_scene scene); +// Memoire +void *ft_realloc(void *ptr, \ + size_t old_size, size_t new_size); #endif diff --git a/peripherique.c b/peripherique.c index 3768f74..b85bc59 100644 --- a/peripherique.c +++ b/peripherique.c @@ -6,7 +6,7 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/17 19:22:27 by yantoine #+# #+# */ -/* Updated: 2025/02/25 15:38:46 by yantoine ### ########.fr */ +/* Updated: 2025/02/25 19:06:52 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,6 +22,7 @@ int key_press(int keycode, t_app *app) mlx_destroy_window(app->mlx, app->win); mlx_destroy_display(app->mlx); free(app->mlx); + free(app->scene.all_file); exit(0); } return (0); @@ -35,6 +36,8 @@ int handle_close(void *param) mlx_destroy_image(app->mlx, app->img); mlx_destroy_window(app->mlx, app->win); mlx_destroy_display(app->mlx); + ft_free_array(app->scene.all_file); + free(app->scene.all_file); free(app->mlx); exit(0); return (0); diff --git a/tags b/tags index 29d7d45..cb26a8d 100644 --- a/tags +++ b/tags @@ -269,6 +269,8 @@ all makefile /^all: $(NAME)$/;" t all minilibx-linux/Makefile /^all : do_configure$/;" t all minilibx-linux/Makefile.mk /^all : $(NAME)$/;" t all minilibx-linux/test/Makefile.mk /^all: $(NAME)$/;" t +all_file miniRT.h /^ char **all_file;$/;" m struct:s_scene typeref:typename:char ** +allocate_memory ft_realloc.c /^static void *allocate_memory(size_t new_size)$/;" f typeref:typename:void * file: ambient miniRT.h /^ t_ambient ambient;$/;" m struct:s_scene typeref:typename:t_ambient aspect miniRT.h /^ float aspect;$/;" m struct:s_calc typeref:typename:float at_exit minilibx-linux/test/run_tests.sh /^at_exit() {$/;" f @@ -322,6 +324,7 @@ color_map_2 minilibx-linux/test/main.c /^int color_map_2(unsigned char *data,int compute_cap_intersection parsing_cylinder_utils.c /^void compute_cap_intersection(t_ray ray, t_cylinder cy, t_calc *calc)$/;" f typeref:typename:void compute_hit_normal parsing_cylinder_utils.c /^void compute_hit_normal(t_ray ray, t_cylinder cy, t_calc *calc,$/;" f typeref:typename:void compute_side_intersection parsing_cylinder_utils.c /^void compute_side_intersection(t_cylinder cy, t_calc *calc)$/;" f typeref:typename:void +copy_memory ft_realloc.c /^static void copy_memory(void *dst, void *src, size_t size)$/;" f typeref:typename:void file: count_word libft/ft_split.c /^static size_t count_word(char *str, char c)$/;" f typeref:typename:size_t file: counter libft/libft.h /^ size_t counter;$/;" m struct:s_info typeref:typename:size_t cp miniRT.h /^ t_vec3 cp;$/;" m struct:s_calc typeref:typename:t_vec3 @@ -391,7 +394,7 @@ ft_putchar_fd libft/ft_putchar_fd.c /^void ft_putchar_fd(char c, int fd)$/;" f t ft_putendl_fd libft/ft_putendl_fd.c /^void ft_putendl_fd(char *s, int fd)$/;" f typeref:typename:void ft_putnbr_fd libft/ft_putnbr_fd.c /^void ft_putnbr_fd(int n, int fd)$/;" f typeref:typename:void ft_putstr_fd libft/ft_putstr_fd.c /^void ft_putstr_fd(char *s, int fd)$/;" f typeref:typename:void -ft_realloc libft/ft_realloc.c /^void *ft_realloc(void *ptr, size_t new_size)$/;" f typeref:typename:void * +ft_realloc ft_realloc.c /^void *ft_realloc(void *ptr, size_t old_size, size_t new_size)$/;" f typeref:typename:void * ft_set_digit libft/ft_itoa.c /^static void ft_set_digit(long nbr_digit, long index, char *nbr, long n)$/;" f typeref:typename:void file: ft_set_digit libft/ft_putnbr_fd.c /^static void ft_set_digit(long nbr_digit, long n, int fd)$/;" f typeref:typename:void file: ft_set_number libft/ft_itoa.c /^static void ft_set_number(char *tab)$/;" f typeref:typename:void file: @@ -416,6 +419,8 @@ ft_toupper libft/ft_toupper.c /^int ft_toupper(int c)$/;" f typeref:typename:int gc minilibx-linux/mlx_int.h /^ GC gc;$/;" m struct:s_win_list typeref:typename:GC gc minilibx-linux/mlx_int.h /^ GC gc;$/;" m struct:s_img typeref:typename:GC gere_mouse minilibx-linux/test/new_win.c /^int gere_mouse(int x,int y,int button,void*toto)$/;" f typeref:typename:int +get_all_file config.c /^static char **get_all_file(int fd)$/;" f typeref:typename:char ** file: +get_copy_size ft_realloc.c /^static size_t get_copy_size(size_t old_size, size_t new_size)$/;" f typeref:typename:size_t file: get_next_line libft/gnl.c /^char *get_next_line(int fd)$/;" f typeref:typename:char * get_tokens_secure parsing_utils.c /^char **get_tokens_secure(t_scene scene, const int numObject,$/;" f typeref:typename:char ** get_xlib_include_path minilibx-linux/configure /^get_xlib_include_path(){$/;" f