From 2fb4c739b231cb8168a1be672c659ff41f65b430 Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Mon, 20 Feb 2023 19:26:08 +0100 Subject: [PATCH] moved deallocation into communicate_thread --- src/chat/server/main.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/chat/server/main.c b/src/chat/server/main.c index f29ece8..6bfd2dd 100644 --- a/src/chat/server/main.c +++ b/src/chat/server/main.c @@ -27,7 +27,6 @@ int main(void) { int listen_socket = socket(AF_INET, SOCK_STREAM, 0); //CREATE SERVER SOCKET int accepted; - char *received = NULL; pthread_t thread; if (listen_socket < 0) why2_die("Failed creating socket."); @@ -54,9 +53,6 @@ int main(void) pthread_create(&thread, NULL, communicate_thread, &accepted); } - //DEALLOCATION - why2_deallocate(received); - return 0; } @@ -65,10 +61,11 @@ void *communicate_thread(void *arg) printf("User connected.\t%d\n", *((int*) arg)); const time_t startTime = time(NULL); + char *received = NULL; while (time(NULL) - startTime < 86400) //KEEP COMMUNICATION ALIVE FOR 24 HOURS { - char *received = read_socket(*((int*) arg)); //READ + received = read_socket(*((int*) arg)); //READ if (received == NULL) return NULL; //FAILED; EXIT THREAD @@ -81,7 +78,10 @@ void *communicate_thread(void *arg) printf("User exited.\t%d\n", *((int*) arg)); + //DEALLOCATION close(*((int*) arg)); + why2_deallocate(received); + return NULL; }