From 8a6a0ba3ee3167a3c3ca2f550c04b9fa255592b5 Mon Sep 17 00:00:00 2001 From: Meutel Date: Fri, 30 Dec 2016 15:54:32 +0100 Subject: [PATCH] Free author mem --- db.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/db.c b/db.c index 10978fb..5c4e69e 100644 --- a/db.c +++ b/db.c @@ -77,6 +77,13 @@ db_book_unfill(Book *p) db_series_unfill(&p->s); } +static void +db_author_unfill(Author *p) { + if (NULL == p) + return; + free(p->name); +} + void db_book_free(Book *p) { @@ -88,7 +95,9 @@ void db_book_adv_free(BookAdv *p) { db_book_unfill(&p->b); - //TODO free authors + for (size_t i = 0; i < p->asize; i++) { + db_author_unfill(p->a[i]); + } free(p); } @@ -114,7 +123,9 @@ db_books_load(struct kreq *r, BookAdv **books, int limit) } struct ksqlstmt *stmt; struct bookauth *p = NULL; + struct bookauth *item = NULL; int i, count = 0; + int64_t bid; ksql_stmt_alloc(r->arg, &stmt, stmts[STMT_BOOKS], @@ -134,11 +145,11 @@ db_books_load(struct kreq *r, BookAdv **books, int limit) ksql_bind_int(stmt, 0, limit); while (KSQL_ROW == ksql_stmt_step(stmt)) { // add author to list, link related book and increment asize - struct bookauth *item = kcalloc(1, sizeof(struct bookauth)); + item = kcalloc(1, sizeof(struct bookauth)); item->a = kcalloc(1, sizeof(Author)); item->a->id = ksql_stmt_int(stmt, 0); item->a->name = kstrdup(ksql_stmt_str(stmt, 1)); - int64_t bid = ksql_stmt_int(stmt, 2); + bid = ksql_stmt_int(stmt, 2); for (i = 0; i < count && NULL == item->b; i++) { if (books[i]->b.id == bid) { item->b = books[i]; @@ -157,9 +168,10 @@ db_books_load(struct kreq *r, BookAdv **books, int limit) } p->b->a[p->b->asize] = p->a;// add author p->b->asize++; + item = p; p = p->p; + free(item); } - // TODO free linked list (not authors) ksql_stmt_free(stmt); return count;