2011-06-04 57 views
-2

有人可以幫我解釋爲什麼這段代碼在泄漏,我們該如何處理它?iphone:奇怪的漏洞

sqlite3 *database; 
if (pickerList) { 
    self.pickerList=nil; 
    [pickerList release]; 

} 
self.pickerList=[[NSMutableArray alloc] init]; 


NSString *dbPath = [self applicationDocumentsDirectory]; 

dbPath=[dbPath stringByAppendingPathComponent:@"database"]; 
dbPath=[dbPath stringByAppendingPathComponent:@"OFFENDERSDB.sqlite"]; 



if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) { 

    if (isAlertForViolationPicker) { 


     const char *sqlStatement = "SELECT * FROM VIOLATIONS_TBL"; 

     sqlite3_stmt *compiledStatement; 

     if (sqlite3_prepare(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) { 

      while (sqlite3_step(compiledStatement) == SQLITE_ROW) { 

       NSString *recSTR=[[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]; 

       [self.pickerList addObject:recSTR]; 
       [recSTR release]; 
       recSTR=nil;    

      } 
     } 
     //[tempRowArray release]; 
     sqlite3_finalize(compiledStatement); 
     //sqlite3_reset(compiledStatement); 
     sqlite3_close(database); 

    } 
    else { 

     const char *sqlStatement = "SELECT * FROM PLAN_TBL"; 
     sqlite3_stmt *compiledStatement; 

     if (sqlite3_prepare(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) { 

      while (sqlite3_step(compiledStatement) == SQLITE_ROW) { 


       NSString *recSTR=[[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]; 

       [self.pickerList addObject:recSTR]; 
       [recSTR release]; 
       recSTR=nil;   

      } 
     } 
     sqlite3_finalize(compiledStatement); 
     sqlite3_close(database); 

    } 






} 

sqlite3_reset(compiledStatement); 

recSTR在這種情況下漏水,我已經嘗試了所有下面提到的解決方案,但沒有工作(更新代碼) Thanx提前

+4

你不會在這裏泄漏'recSTR'。漏洞很可能與該字符串涉及'pickerList'有關。這個代碼是不夠的,以弄清楚。 – 2011-06-04 18:53:25

+0

Double post:http://stackoverflow.com/questions/6235632/iphone-memory-leak-while-reading-data-in-loop-from-datatbase – dasdom 2011-06-04 18:56:28

+1

儀器只告訴你泄漏對象的創建位置,而不是它在哪裏被泄露。 – albertamg 2011-06-04 19:04:25

回答

1

看起來好像你可能會泄漏pickerList。你有一個指向pickerList的指針,然後你將它設置爲nil。然後你發送釋放消息到這一點(這實際上是一個沒有操作)。如果你使用:

if (pickerList) 
{ 
    [pickerList release]; 
    self.pickerList=nil; 
} 

而不是你現在的代碼,你會更好嗎?沒有看到更多的代碼很難說,但你肯定想在你把伊娃爾設置爲零之前釋放。 (這表示如果你已經完成@property(retain)UIPickerList * pickerList then self.pickerList = nil將釋放pickerList。如果你已經完成了這個操作,那麼你的[pickerList release]調用是多餘的。)

你可能以及從儀器泄漏recSTR泄漏的報告。但這並不意味着問題不在pickerList中。查看代碼,recSTR不是不可能被pickerList的一個實例所擁有,因爲您已經放棄了指向它的指針,然後將釋放消息發送給nil。所以你最終會泄漏recSTR和pickerList。