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