preventing memleaks on exit in why2_communicate_thread loop

This commit is contained in:
Václav Šmejkal 2023-04-01 13:36:08 +02:00
parent ed8b328673
commit 0132c00f46
Signed by: ENGO150
GPG Key ID: 4A57E86482968843

View File

@ -31,6 +31,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <why2/chat/flags.h> #include <why2/chat/flags.h>
#include <why2/memory.h> #include <why2/memory.h>
#include <why2/misc.h>
//LINKED LIST STUFF (BIT CHANGED memory.c'S VERSION) //LINKED LIST STUFF (BIT CHANGED memory.c'S VERSION)
typedef struct node typedef struct node
@ -307,13 +308,14 @@ void *why2_communicate_thread(void *arg)
push_to_list(connection, pthread_self()); //ADD TO LIST push_to_list(connection, pthread_self()); //ADD TO LIST
const time_t startTime = time(NULL); time_t start_time = time(NULL);
char *received = NULL; char *received = NULL;
char *raw = NULL; char *raw = NULL;
char *decoded_buffer; char *decoded_buffer;
pthread_t thread_buffer; pthread_t thread_buffer;
why2_bool exiting = 0;
while (time(NULL) - startTime < 86400) //KEEP COMMUNICATION ALIVE FOR 24 HOURS while ((time(NULL) - start_time) < WHY2_COMMUNICATION_TIME && !exiting) //KEEP COMMUNICATION ALIVE FOR 5 MINUTES [RESET TIMER AT MESSAGE SENT]
{ {
//READ //READ
raw = read_socket_raw(connection); raw = read_socket_raw(connection);
@ -334,9 +336,9 @@ void *why2_communicate_thread(void *arg)
if (decoded_buffer[0] == '!') //COMMANDS if (decoded_buffer[0] == '!') //COMMANDS
{ {
if (strcmp(decoded_buffer, "!exit") == 0) break; //USER REQUESTED EXIT if (strcmp(decoded_buffer, "!exit") == 0) exiting = 1; //USER REQUESTED EXIT
continue; //IGNORE MESSAGES BEGINNING WITH '!' goto deallocation; //IGNORE MESSAGES BEGINNING WITH '!'
} }
printf("Received:\n%s\n\n", received); printf("Received:\n%s\n\n", received);
@ -344,6 +346,11 @@ void *why2_communicate_thread(void *arg)
pthread_create(&thread_buffer, NULL, send_to_all, raw); pthread_create(&thread_buffer, NULL, send_to_all, raw);
pthread_join(thread_buffer, NULL); pthread_join(thread_buffer, NULL);
//RESET TIMER
start_time = time(NULL);
deallocation:
why2_deallocate(received); why2_deallocate(received);
why2_deallocate(raw); why2_deallocate(raw);
why2_deallocate(decoded_buffer); why2_deallocate(decoded_buffer);