From 29312b7a7c73d926dea8e8abdd9f1bd5f6169fc3 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Tue, 19 Mar 2024 11:23:36 +0100 Subject: [PATCH] Fix leak caused by not accepting thread's return The threads have alocatted memory where return value resides. A thread has to be joined (or detached) to release this memory. --- philo/main.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/philo/main.c b/philo/main.c index 31fb432..1626f83 100644 --- a/philo/main.c +++ b/philo/main.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 12:49:40 by ljiriste #+# #+# */ -/* Updated: 2024/03/07 17:51:19 by ljiriste ### ########.fr */ +/* Updated: 2024/03/19 11:20:03 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -249,7 +249,7 @@ int parse_input(int argc, char **argv, t_settings *set) int res; if (argc != 5 && argc != 6) - return (0); + return (1); res = 0; res = res || parse_args(argv[1], &set->philo_count); res = res || parse_arg(argv[2], &set->time_to_die); @@ -270,6 +270,9 @@ int init_settings(int argc, char **argv, t_settings *set) } +// "pthread_detach(threads[count]);" should be sufficient +// instead of pthread_join(thread[count], NULL) but valgrind +// then shows leaks void cleanup(pthread_mutex_t *forks, t_philo *philos, pthread_t *threads, t_settings *set) { size_t count; @@ -279,6 +282,7 @@ void cleanup(pthread_mutex_t *forks, t_philo *philos, pthread_t *threads, t_sett { --count; pthread_mutex_destroy(&forks[count]); + pthread_join(threads[count], NULL); } pthread_mutex_destroy(&set->terminal); free(forks); @@ -335,7 +339,10 @@ int main(int argc, char **argv) t_philo *philos; if (init_settings(argc, argv, &settings)) + { + printf("Invalid input arguments.\n"); return (1); + } threads = malloc(sizeof(pthread_t) * settings.philo_count); forks = init_forks(settings.philo_count); philos = build_philos(forks, &settings); -- 2.30.2