vensan
vensan

Reputation: 319

Xcode memory leak when using SQLite3 API

i am using the following code snippet to add new element/row with name to Sqlite3 database, Every thing works fine, But this is giving memory leaks each time, when i call this function, Can any one help me how to avoid this problem?

{

    sqlite3 *database;

sqlite3_stmt *addStmt;
    NSString *localdescription=@"Enter your Notes here";

if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) 
{
    const char *sql = "insert into database(name) Values(?)";
    if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) == SQLITE_OK)
    {

    sqlite3_bind_text(addStmt, 1, [localName UTF8String], -1, SQLITE_TRANSIENT);


    if(SQLITE_DONE != sqlite3_step(addStmt))
        NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
    else
        //SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
        NSLog(@"id=====%d",sqlite3_last_insert_rowid(database));

    //Reset the add statement.
    sqlite3_reset(addStmt);


    }

        }
    sqlite3_close(database);
}

Upvotes: 4

Views: 1047

Answers (1)

rckoenes
rckoenes

Reputation: 69469

You should finalize any prepared statement that you no longer use:

sqlite3_finalize(addStmt), addStmt = nil;

There is no real need to set the pointer to nil I just real like it.

Upvotes: 2

Related Questions