2012-02-26 7 views
1

sqliteを使用しているときに6MBのメモリリークがあります。今私はgetBookWithIdTestメソッドをテストしています。sqliteで作業しているときに6MBのメモリリークがあります

-(IBAction) onTest:(id)sender 
{ 
NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init]; 
for (int i=0; i<100; i++) 
{ 
    [DatabaseManager getBookWithIdTest:i]; 
} 
[myPool drain];  
} 

私は6 MBのメモリをリークしています。しかし、なぜ?

+ (BookSettings *)getBookWithIdTest:(int)abookId 
{ 
    BookSettings *book = [[[BookSettings alloc] init] autorelease];  
    sqlite3 *database; 

    if(sqlite3_open([DatabaseManager databasePath], &database) == SQLITE_OK) 
    { 
    sqlite3_stmt *compiledStatement; 

    //FIRST PART 
    const char *sqlStatementBook = [[NSString stringWithFormat:@"SELECT * FROM t_abooks"] cStringUsingEncoding:NSASCIIStringEncoding]; 

    if(sqlite3_prepare_v2(database, sqlStatementBook, -1, &compiledStatement, NULL) == SQLITE_OK) 
    {    
     while(sqlite3_step(compiledStatement) == SQLITE_ROW) 
     {    
     }    
    } 
    else NSLog(@"sqlite3_prepare_v2 error %s", sqlite3_errmsg(database)); 

    sqlite3_reset(compiledStatement); 
    //END FIRST PART 
    // SECOND PART 
    const char *sqlStatementAuthors = [[NSString stringWithFormat:@"SELECT * FROM t_authors"] cStringUsingEncoding:NSASCIIStringEncoding]; 

    if(sqlite3_prepare_v2(database, sqlStatementAuthors, -1, &compiledStatement, NULL) == SQLITE_OK) 
    { 
     while(sqlite3_step(compiledStatement) == SQLITE_ROW) 
     {     
     }    
    } 
    else NSLog(@"sqlite3_prepare_v2 error %s", sqlite3_errmsg(database)); 

    sqlite3_reset(compiledStatement); 
    //END SECOND PART 
    sqlite3_finalize(compiledStatement); 
} else NSLog(@"sqlite3_open error"); 

sqlite3_close(database); 
return book; 
} 

しかし、私が最初の部分または2番目の部分を削除した場合、私は漏れはありません。 たとえば

+ (BookSettings *)getBookWithIdTest:(int)abookId 
{ 
BookSettings *book = [[[BookSettings alloc] init] autorelease];  
sqlite3 *database; 

if(sqlite3_open([DatabaseManager databasePath], &database) == SQLITE_OK) 
{ 
    sqlite3_stmt *compiledStatement; 

    //FIRST PART -removed   
    // SECOND PART 
    const char *sqlStatementAuthors = [[NSString stringWithFormat:@"SELECT * FROM t_authors"] cStringUsingEncoding:NSASCIIStringEncoding]; 

    if(sqlite3_prepare_v2(database, sqlStatementAuthors, -1, &compiledStatement, NULL) == SQLITE_OK) 
    { 
     while(sqlite3_step(compiledStatement) == SQLITE_ROW) 
     {     
     }    
    } 
    else NSLog(@"sqlite3_prepare_v2 error %s", sqlite3_errmsg(database)); 

    sqlite3_reset(compiledStatement); 

    sqlite3_finalize(compiledStatement); 
} else NSLog(@"sqlite3_open error"); 

sqlite3_close(database); 
return book; 
} 

答えて

4

私はこの問題を解決しました。追加する必要があります

sqlite3_finalize(compiledStatement); 

最初の部分の最後に追加する必要があります。

関連する問題