2014-01-17 45 views
0

我在我的儀器工具在iPhone中獲取sqlite3Memmalloc泄漏。我的應用消耗超過200 MB的內存。在內存管理工具中,它顯示責任庫爲libsqlite3.dylib &負責調用者爲sqlite3MemMalloc。在我的應用程序中,我試圖插入2000行。我認爲由於內存使用情況,應用程序崩潰。我檢查了所有方面,但無法找到我的sqlite代碼中的錯誤泄漏。請給我一個解決方案。以下是我的sqlite代碼。內存泄漏iPhone應用程序中的sqlite3MemMalloc

- (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; 
} 

回答

2

有很多在該代碼可怕錯誤的,​​但相關的內存泄漏的是:

  • 你並不總是叫sqlite3_finalizesqlite3_prepare_v2後成功;和
  • sqlite3_open成功後,您並不總是撥打sqlite3_close

您不能在某個if/else分支中隱藏這些呼叫。 重構您的代碼,以便始終進行這些調用。