yozhik
yozhik

Reputation: 5074

sqlite3_exec is there a memory leak?

I'm using SQLite to store my data. I'm writing wrapper class, and I want to know: will be a memory leak if (res != SQLITE_OK) and errorMsg will be displayed to the screen??

So do I need to do free(errorMsg); in the "if" statement? Thanx!

-(int) executeQuery: (NSString *) sqlQueryStr
{
char *errorMsg = NULL;
int res = SQLITE_ERROR;

res = sqlite3_exec(database, [sqlQueryStr UTF8String], NULL, NULL, &errorMsg);

if (res != SQLITE_OK)
{
    sqlite3_close(database); 
    NSLog(@"executeQuery Error:  %@", errorMsg);
    database = NULL;
    return res;
}

return res;
}

Upvotes: 2

Views: 3584

Answers (1)

Daniel Dickison
Daniel Dickison

Reputation: 21882

You should use sqlite3_free() to release the error message string, as per the documentation:

To avoid memory leaks, the application should invoke sqlite3_free() on error message strings returned through the 5th parameter of of sqlite3_exec() after the error message string is no longer needed.

Upvotes: 4

Related Questions