From 3edbce0c1893d6cd0ba736630678c7dcc66a0861 Mon Sep 17 00:00:00 2001 From: Alexandre Ratchov Date: Mon, 15 Oct 2012 18:43:34 +0200 Subject: [PATCH] fix memory leaks --- sndiod/listen.c | 5 +++-- sndiod/midi.c | 11 +++++++++-- sndiod/opt.c | 17 +++++++++++++++++ sndiod/opt.h | 2 +- sndiod/sndiod.c | 3 +++ sndiod/utils.c | 12 ++++++++++++ sndiod/utils.h | 2 ++ 7 files changed, 47 insertions(+), 5 deletions(-) diff --git a/sndiod/listen.c b/sndiod/listen.c index cf0d246..9725936 100644 --- a/sndiod/listen.c +++ b/sndiod/listen.c @@ -73,10 +73,11 @@ listen_close(struct listen *f) if (f->path != NULL) { unlink(f->path); - free(f->path); + xfree(f->path); } file_del(f->file); close(f->fd); + xfree(f); } void @@ -108,7 +109,7 @@ listen_new_un(char *path) f->file = file_new(&listen_fileops, f, path, 1); if (f->file == NULL) goto bad_close; - f->path = strdup(path); + f->path = xstrdup(path); if (f->path == NULL) { perror("strdup"); exit(1); diff --git a/sndiod/midi.c b/sndiod/midi.c index cc15356..d175a75 100644 --- a/sndiod/midi.c +++ b/sndiod/midi.c @@ -350,7 +350,7 @@ midi_out(struct midi *oep, unsigned char *idata, int icount) void port_log(struct port *p) { - log_puts(p->path); + midi_log(p->midi); } #endif @@ -440,8 +440,15 @@ int port_open(struct port *c) { c->mio = miofile_new(c); - if (c->mio == NULL) + if (c->mio == NULL) { + if (log_level >= 1) { + port_log(c); + log_puts(": "); + log_puts(c->path); + log_puts(": failed to open midi port\n"); + } return 0; + } c->state = PORT_INIT; return 1; } diff --git a/sndiod/opt.c b/sndiod/opt.c index 002c343..ea5bf5f 100644 --- a/sndiod/opt.c +++ b/sndiod/opt.c @@ -119,3 +119,20 @@ opt_byname(char *name, unsigned int num) } return NULL; } + +void +opt_del(struct opt *o) +{ + struct opt **po; + + for (po = &opt_list; *po != o; po = &(*po)->next) { +#ifdef DEBUG + if (*po == NULL) { + log_puts("opt_del: not on list\n"); + panic(); + } +#endif + } + *po = o->next; + xfree(o); +} diff --git a/sndiod/opt.h b/sndiod/opt.h index 95b6865..bf71c21 100644 --- a/sndiod/opt.h +++ b/sndiod/opt.h @@ -36,7 +36,7 @@ extern struct opt *opt_list; struct opt *opt_new(char *, struct dev *, int, int, int, int, int, int, int, unsigned int); -int opt_bind(struct opt *); +void opt_del(struct opt *); struct opt *opt_byname(char *, unsigned int); #endif /* !defined(OPT_H) */ diff --git a/sndiod/sndiod.c b/sndiod/sndiod.c index b21bbae..cfaae25 100644 --- a/sndiod/sndiod.c +++ b/sndiod/sndiod.c @@ -471,6 +471,8 @@ main(int argc, char **argv) listen_close(listen_list); while (sock_list != NULL) sock_close(sock_list); + while (opt_list != NULL) + opt_del(opt_list); for (d = dev_list; d != NULL; d = d->next) dev_done(d); for (p = port_list; p != NULL; p = p->next) @@ -485,5 +487,6 @@ main(int argc, char **argv) filelist_done(); rmdir(base); unsetsig(); + mem_stats(); return 0; } diff --git a/sndiod/utils.c b/sndiod/utils.c index a6787de..96c161d 100644 --- a/sndiod/utils.c +++ b/sndiod/utils.c @@ -253,3 +253,15 @@ mem_stats(void) log_puts("\n"); } } + +char * +mem_strdup(const char *s, char *tag) +{ + size_t size; + void *p; + + size = strlen(s) + 1; + p = mem_alloc(size, tag); + memcpy(p, s, size); + return p; +} diff --git a/sndiod/utils.h b/sndiod/utils.h index c955e6b..d7de231 100644 --- a/sndiod/utils.h +++ b/sndiod/utils.h @@ -28,10 +28,12 @@ void log_flush(void); //void *xmalloc(size_t); void *mem_alloc(size_t, char *); +char *mem_strdup(const char *, char *); void mem_free(void *); void mem_stats(void); #define xmalloc(s) (mem_alloc((s), (char *)__func__)) +#define xstrdup(s) (mem_strdup((s), (char *)__func__)) #define xfree(p) (mem_free((p))) void memrnd(void *, size_t);