Fixed a memory leak in the output thread function by moving the msg malloc after where the thread waits for something to print. If the thread gets cancelled while waiting on this cv, msg never gets properly freed.
This commit is contained in:
@@ -29,12 +29,10 @@ void *input(void *arg) {
|
|||||||
char *msg;
|
char *msg;
|
||||||
struct ncinput ni;
|
struct ncinput ni;
|
||||||
uint32_t character;
|
uint32_t character;
|
||||||
int i, savedsize;
|
int i;
|
||||||
CONTEXT *context = (CONTEXT *)arg;
|
CONTEXT *context = (CONTEXT *)arg;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
/* This is the message that will be saved to history linked list */
|
|
||||||
char *savedmsg;
|
|
||||||
/* Get a message from the user */
|
/* Get a message from the user */
|
||||||
msg = malloc(512);
|
msg = malloc(512);
|
||||||
memset(msg, 0, 512);
|
memset(msg, 0, 512);
|
||||||
@@ -71,6 +69,9 @@ void *input(void *arg) {
|
|||||||
msg[i++] = (char)character;
|
msg[i++] = (char)character;
|
||||||
msg[i] = '\0';
|
msg[i] = '\0';
|
||||||
}
|
}
|
||||||
|
/*else if (i == 511) {
|
||||||
|
break;
|
||||||
|
}*/
|
||||||
|
|
||||||
/* Print the message as the user types it */
|
/* Print the message as the user types it */
|
||||||
ncplane_erase_region(context->plane_prompt, 0, 0, 1, context->ctx_cols);
|
ncplane_erase_region(context->plane_prompt, 0, 0, 1, context->ctx_cols);
|
||||||
@@ -85,13 +86,14 @@ void *input(void *arg) {
|
|||||||
if (ListCount(context->ctx_sendlist) == 1) {
|
if (ListCount(context->ctx_sendlist) == 1) {
|
||||||
pthread_cond_signal(&context->cv_send);
|
pthread_cond_signal(&context->cv_send);
|
||||||
}
|
}
|
||||||
|
free(msg);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Take an item off the send list and send it to the other user */
|
/* Take an item off the send list and send it to the other user */
|
||||||
void *sender(void *arg) {
|
void *sender(void *arg) {
|
||||||
char *msg, *header, *savedmsg;
|
char *msg, *header;
|
||||||
int bytes;
|
int bytes;
|
||||||
CONTEXT *context = (CONTEXT *)arg;
|
CONTEXT *context = (CONTEXT *)arg;
|
||||||
|
|
||||||
@@ -132,14 +134,14 @@ void *sender(void *arg) {
|
|||||||
strcpy(header, "You: ");
|
strcpy(header, "You: ");
|
||||||
strncat(header, msg, strlen(msg));
|
strncat(header, msg, strlen(msg));
|
||||||
|
|
||||||
|
/* Add to receive list. This is done to print the received and
|
||||||
|
* sent messages in order */
|
||||||
pthread_mutex_lock(&context->mut_recv);
|
pthread_mutex_lock(&context->mut_recv);
|
||||||
ListAppend(context->ctx_recvlist, header);
|
ListAppend(context->ctx_recvlist, header);
|
||||||
pthread_mutex_unlock(&context->mut_recv);
|
pthread_mutex_unlock(&context->mut_recv);
|
||||||
|
|
||||||
/* Wake thread waiting for message to print */
|
/* Wake thread waiting for message to print */
|
||||||
if (ListCount(context->ctx_recvlist) == 1) {
|
pthread_cond_signal(&context->cv_recv);
|
||||||
pthread_cond_signal(&context->cv_recv);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -152,9 +154,9 @@ void *receiver(void *arg) {
|
|||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
msg = malloc(512);
|
msg = malloc(512);
|
||||||
header = malloc(strlen(context->ip) + 514);
|
header = malloc(strlen(context->ip) + 515);
|
||||||
memset(msg, 0, 512);
|
memset(msg, 0, 512);
|
||||||
memset(header, 0, strlen(context->ip) + 514);
|
memset(header, 0, strlen(context->ip) + 515);
|
||||||
|
|
||||||
bytes = recv(context->ctx_confd, msg, 512, 0);
|
bytes = recv(context->ctx_confd, msg, 512, 0);
|
||||||
|
|
||||||
@@ -191,7 +193,6 @@ void *output(void *arg) {
|
|||||||
CONTEXT *context = (CONTEXT *)arg;
|
CONTEXT *context = (CONTEXT *)arg;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
msg = malloc(512 + strlen(context->ip));
|
|
||||||
|
|
||||||
/* Block if there's nothing to output */
|
/* Block if there's nothing to output */
|
||||||
if (ListCount(context->ctx_recvlist) == 0) {
|
if (ListCount(context->ctx_recvlist) == 0) {
|
||||||
@@ -200,6 +201,7 @@ void *output(void *arg) {
|
|||||||
pthread_mutex_unlock(&context->mut_recvcv);
|
pthread_mutex_unlock(&context->mut_recvcv);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
msg = malloc(515 + strlen(context->ip));
|
||||||
/* Remove a message from the list and print it to chatlog */
|
/* Remove a message from the list and print it to chatlog */
|
||||||
pthread_mutex_lock(&context->mut_recv);
|
pthread_mutex_lock(&context->mut_recv);
|
||||||
ListFirst(context->ctx_recvlist);
|
ListFirst(context->ctx_recvlist);
|
||||||
@@ -208,11 +210,11 @@ void *output(void *arg) {
|
|||||||
pthread_mutex_unlock(&context->mut_recv);
|
pthread_mutex_unlock(&context->mut_recv);
|
||||||
|
|
||||||
ncplane_printf(context->plane_log, "%s\n", msg);
|
ncplane_printf(context->plane_log, "%s\n", msg);
|
||||||
|
|
||||||
pthread_mutex_lock(&context->mut_render);
|
pthread_mutex_lock(&context->mut_render);
|
||||||
notcurses_render(context->ctx_nc);
|
notcurses_render(context->ctx_nc);
|
||||||
pthread_mutex_unlock(&context->mut_render);
|
pthread_mutex_unlock(&context->mut_render);
|
||||||
|
|
||||||
|
free(msg);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -239,7 +241,7 @@ int main(int argc, char* argv[]) {
|
|||||||
int yes = 1;
|
int yes = 1;
|
||||||
|
|
||||||
/* threading stuff */
|
/* threading stuff */
|
||||||
LIST *receivelist, *sendlist, *history;
|
LIST *receivelist, *sendlist;
|
||||||
pthread_t input_thread, send_thread, receive_thread, output_thread;
|
pthread_t input_thread, send_thread, receive_thread, output_thread;
|
||||||
pthread_mutex_t sendmut,
|
pthread_mutex_t sendmut,
|
||||||
receivemut,
|
receivemut,
|
||||||
@@ -413,7 +415,6 @@ int main(int argc, char* argv[]) {
|
|||||||
/* Linked lists to hold messages to be sent and printed */
|
/* Linked lists to hold messages to be sent and printed */
|
||||||
receivelist = ListCreate();
|
receivelist = ListCreate();
|
||||||
sendlist = ListCreate();
|
sendlist = ListCreate();
|
||||||
history = ListCreate();
|
|
||||||
|
|
||||||
/* notcurses init */
|
/* notcurses init */
|
||||||
if (!setlocale(LC_ALL, "")) {
|
if (!setlocale(LC_ALL, "")) {
|
||||||
@@ -492,7 +493,6 @@ int main(int argc, char* argv[]) {
|
|||||||
close(confd);
|
close(confd);
|
||||||
ListFree(receivelist);
|
ListFree(receivelist);
|
||||||
ListFree(sendlist);
|
ListFree(sendlist);
|
||||||
ListFree(history);
|
|
||||||
notcurses_stop(nc);
|
notcurses_stop(nc);
|
||||||
printf("%s\n", closed);
|
printf("%s\n", closed);
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user