Reputation: 1
I am getting sqlite3Memmalloc leak in my instruments tool in iphone. My app is consuming memory of more than 200 MB. In the memory management instrument it shows Responsible Library as libsqlite3.dylib & Responsible caller as sqlite3MemMalloc. In my app I am trying to insert 2000 rows.I think due to the memory usage the app gets crashed. I have checked in all aspects but not able to find the error leak in my sqlite code. Please provide me a solution. Below is my sqlite code.
- (Boolean) insertQuotes:(QuotesInfo*)quote
{
NSString *dbPath = [dBManager GetConnectToDatabase];
sqlite3_stmt *selectstmt=nil;
if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
if(selectstmt == nil)
{
NSString* Query=[NSString stringWithFormat:@"insert into Quotes (QuoteID,QuoteDate,QuoteDesc,QuoteAuthor,QuoteCategory,QuoteCategoryID,QuoteAuthorID,QuoteFavourite,QuoteCategoryFilterStatus,QuoteAuthorFilterStatus,AuthorFirstName,AuthorLastName) values (%d, '%@', %@, '%@', '%@', %d, %d, %d, %d, %d, '%@', '%@')",quote.quoteId,quote.quoteDate,quote.quoteDesc,quote.quoteAuthor,quote.quoteCategory,quote.quoteCategoryID,quote.quoteAuthorID,quote.quoteFav,quote.quoteCategoryFilterStatus,quote.quoteAuthorFilterStatus,quote.authorFirstName,quote.authorLastName];
NSLog(@"Query %@",Query);
const char *sql = [Query UTF8String];
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) != SQLITE_OK)
{
NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
}
}
@try {
}
@catch (NSException * e) {
return NO;
NSLog(@"INSERT EXCEPTION %@",e);
//Handle the Exception here..
}
if(SQLITE_DONE != sqlite3_step(selectstmt)){
NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
}
else{
sqlite3_reset(selectstmt);
sqlite3_finalize(selectstmt);
sqlite3_close(database);
return YES;
}
}
return NO;
}
Upvotes: 0
Views: 1570
Reputation: 180290
There are lots of horrible bugs in that code, but the ones related to memory leaks are:
sqlite3_finalize
after sqlite3_prepare_v2
has succeeded; andsqlite3_close
after sqlite3_open
has succeeded.You must not hide those calls in some if
/else
branch.
Restructure your code so that these calls are always made.
Upvotes: 2