hagope
hagope

Reputation: 5531

my sqlite3 c function is leaking memory

Any idea why this function is leaking memory every time I call it?

char *getData(sqlite3 *db)
{
    char *ret;
    sqlite3_stmt *res;

    int rc = sqlite3_prepare_v2(db, SELECT_STATEMENT_SQL, -1, &res, 0);

    if (rc != SQLITE_OK) {
        sprintf(stderr, "%s\n", sqlite3_errmsg(db));
        return stderr;
    }    

    rc = sqlite3_step(res);

    if (rc == SQLITE_ROW) {
        ret = sqlite3_column_text(res, 0);

    } else {
        ret = "error!";
    }
    sqlite3_free(res);
    return ret;
}

Upvotes: 1

Views: 773

Answers (1)

qrdl
qrdl

Reputation: 34998

You need to call sqlite3_finalize() to properly release memory, allocated for the statement.

Upvotes: 4

Related Questions