Mem cleanup

This commit is contained in:
Meutel 2016-12-30 00:45:20 +01:00
parent 9fb4a9f5cd
commit dcff9e437c
3 changed files with 15 additions and 1 deletions

9
db.c
View File

@ -41,6 +41,15 @@ static const char *const stmts[STMT__MAX] = {
"SELECT books.id AS id,title FROM books LIMIT ?", "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 int
db_books_load(struct kreq *r, struct book **books, int limit) db_books_load(struct kreq *r, struct book **books, int limit)
{ {

View File

@ -29,6 +29,7 @@ __BEGIN_DECLS
struct book * db_book_load(struct kreq *r, int64_t id); struct book * db_book_load(struct kreq *r, int64_t id);
int db_books_load(struct kreq *r, struct book **books, int limit); int db_books_load(struct kreq *r, struct book **books, int limit);
void db_book_free(struct book *b);
void db_close(struct kreq *); void db_close(struct kreq *);
int db_open(struct kreq *, const char *); int db_open(struct kreq *, const char *);

6
main.c
View File

@ -125,13 +125,17 @@ sendbooks(struct kreq *r)
puterror(&req, "Unknown book"); puterror(&req, "Unknown book");
} else if (NULL != b) { } else if (NULL != b) {
putbook(&req, b); putbook(&req, b);
db_book_free(b);
} else { } else {
kjson_array_open(&req); kjson_array_open(&req);
struct book **books = kcalloc(10, sizeof(struct book)); struct book **books = kcalloc(10, sizeof(struct book));
res = db_books_load(r, books, 10); res = db_books_load(r, books, 10);
while (i < res) { while (i < res) {
putbook(&req, books[i++]); putbook(&req, books[i]);
db_book_free(books[i]);
i++;
} }
free(books);
kjson_array_close(&req); kjson_array_close(&req);
} }
kjson_close(&req); kjson_close(&req);