From dcff9e437c6b17bc8b04a93339621b7a353c2571 Mon Sep 17 00:00:00 2001 From: Meutel Date: Fri, 30 Dec 2016 00:45:20 +0100 Subject: [PATCH] Mem cleanup --- db.c | 9 +++++++++ extern.h | 1 + main.c | 6 +++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/db.c b/db.c index e262407..30da7d3 100644 --- a/db.c +++ b/db.c @@ -41,6 +41,15 @@ static const char *const stmts[STMT__MAX] = { "SELECT books.id AS id,title FROM books LIMIT ?", }; +void +db_book_free(struct book *b) +{ + if (NULL == b) + return; + free(b->title); + free(b); +} + int db_books_load(struct kreq *r, struct book **books, int limit) { diff --git a/extern.h b/extern.h index 466c199..53ca5f1 100644 --- a/extern.h +++ b/extern.h @@ -29,6 +29,7 @@ __BEGIN_DECLS struct book * db_book_load(struct kreq *r, int64_t id); int db_books_load(struct kreq *r, struct book **books, int limit); +void db_book_free(struct book *b); void db_close(struct kreq *); int db_open(struct kreq *, const char *); diff --git a/main.c b/main.c index 6654a3f..b691313 100644 --- a/main.c +++ b/main.c @@ -125,13 +125,17 @@ sendbooks(struct kreq *r) puterror(&req, "Unknown book"); } else if (NULL != b) { putbook(&req, b); + db_book_free(b); } else { kjson_array_open(&req); struct book **books = kcalloc(10, sizeof(struct book)); res = db_books_load(r, books, 10); while (i < res) { - putbook(&req, books[i++]); + putbook(&req, books[i]); + db_book_free(books[i]); + i++; } + free(books); kjson_array_close(&req); } kjson_close(&req);