From 40479540b4c8dd6a876b084bce0e6c806033da06 Mon Sep 17 00:00:00 2001 From: Christopher Brannon Date: Mon, 29 Jun 2009 18:33:40 -0500 Subject: [PATCH] Fix a memory leak. If we fail to add entries to the queue in queue_add_cmd or queue_add_text, properly free the entry. --- espeakup.h | 2 +- queue.c | 5 +++-- softsynth.c | 17 ++++++++++++++--- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/espeakup.h b/espeakup.h index 3afbf54..604f240 100644 --- a/espeakup.h +++ b/espeakup.h @@ -70,7 +70,7 @@ extern int debug; extern void process_cli(int argc, char **argv); extern struct queue_t *new_queue(void); -extern void queue_add(struct queue_t *q, void *entry); +extern int queue_add(struct queue_t *q, void *entry); extern void queue_remove(struct queue_t *q); extern void *queue_peek(struct queue_t *q); extern void *signal_thread(void *arg); diff --git a/queue.c b/queue.c index c12ced9..0db6ecf 100644 --- a/queue.c +++ b/queue.c @@ -45,7 +45,7 @@ struct queue_t *new_queue(void) return q; } -void queue_add(struct queue_t *q, void *data) +int queue_add(struct queue_t *q, void *data) { struct queue_entry_t *tmp; @@ -53,7 +53,7 @@ void queue_add(struct queue_t *q, void *data) tmp = malloc(sizeof(struct queue_entry_t)); if (!tmp) { printf("Unable to allocate memory for queue entry.\n"); - return; + return 0; } tmp->data = data; tmp->next = NULL; @@ -65,6 +65,7 @@ void queue_add(struct queue_t *q, void *data) } if (!q->head) q->head = tmp; + return 1; } void queue_remove(struct queue_t *q) diff --git a/softsynth.c b/softsynth.c index 27b466c..2c19023 100644 --- a/softsynth.c +++ b/softsynth.c @@ -38,6 +38,7 @@ static int softFD = 0; static void queue_add_cmd(enum command_t cmd, enum adjust_t adj, int value) { struct espeak_entry_t *entry; + int added = 0; entry = malloc(sizeof(struct espeak_entry_t)); if (!entry) { @@ -48,7 +49,10 @@ static void queue_add_cmd(enum command_t cmd, enum adjust_t adj, int value) entry->adjust = adj; entry->value = value; pthread_mutex_lock(&queue_guard); - queue_add(synth_queue, (void *) entry); + added = queue_add(synth_queue, (void *) entry); + if (!added) + free(entry); + else pthread_cond_signal(&runner_awake); pthread_mutex_unlock(&queue_guard); } @@ -56,6 +60,7 @@ static void queue_add_cmd(enum command_t cmd, enum adjust_t adj, int value) static void queue_add_text(char *txt, size_t length) { struct espeak_entry_t *entry; + int added = 0; entry = malloc(sizeof(struct espeak_entry_t)); if (!entry) { @@ -72,8 +77,14 @@ static void queue_add_text(char *txt, size_t length) } entry->len = length; pthread_mutex_lock(&queue_guard); - queue_add(synth_queue, (void *) entry); - pthread_cond_signal(&runner_awake); + added = queue_add(synth_queue, (void *) entry); + if (!added) { + free(entry->buf); + free(entry); + } else { + pthread_cond_signal(&runner_awake); + } + pthread_mutex_unlock(&queue_guard); }