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.
This commit is contained in:
Christopher Brannon 2009-06-29 18:33:40 -05:00 committed by William Hubbs
commit 40479540b4
3 changed files with 18 additions and 6 deletions

View file

@ -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);

View file

@ -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)

View file

@ -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);
}