2011-07-27 88 views
0

我在我的應用程序中有3個內存泄漏,我找不到如何修復它。我對xcode和目標c很陌生。這裏是我有的代碼:修復內存泄漏的問題iphone

if(sqlite3_prepare_v2([[DatabaseController sharedDatabaseController] getDb], sqlQueryConverted, -1, &dbStatement, NULL)==SQLITE_OK){ 

     //Run the query 
     while (sqlite3_step(dbStatement) == SQLITE_ROW) 
     {  


      const char *name = (const char *)sqlite3_column_text(dbStatement, 0); 
      int courseId = sqlite3_column_int(dbStatement, 1); 
      const char *location = (const char *)sqlite3_column_text(dbStatement, 2); 
      const char *date = (const char *)sqlite3_column_text(dbStatement, 3); 

      //Convert the returnedElement char to string 
      nameConverted = [[NSString alloc] initWithUTF8String:name]; 
      locationConverted = [[NSString alloc] initWithUTF8String:location]; 
      dateConverted = [[NSString alloc] initWithUTF8String:date]; 

      Course *course = [[[Course alloc]initWithName:nameConverted _id:courseId location:locationConverted courseDate:dateConverted] autorelease]; 

      //Add the course to the to a temporary list to remove duplicated items    
      [tempResults addObject:course]; 

     } 
     [nameConverted release]; 
     [locationConverted release]; 
     [dateConverted release]; 
    } 

我試圖autorelease它也是。此代碼用於過濾搜索並重新加載搜索顯示錶。如果我把release行寫入while聲明中,如果我輸入2個字母,應用程序就會崩潰。我怎麼能解決這個問題?

謝謝。

編輯:我一直在這個問題來回奔跑,沒有運氣。我得出的結論是,儀器出現問題,因爲它仍然顯示內存泄漏。下面的代碼因爲它是今天,當我認爲應該解決這個問題:

NSString *nameConverted = [[NSString alloc] initWithUTF8String:name]; 
      NSString *locationConverted = [[NSString alloc] initWithUTF8String:location]; 
      NSString *dateConverted = [[NSString alloc] initWithUTF8String:date]; 

      Course *course = [[[Course alloc]initWithName:nameConverted _id:courseId location:locationConverted courseDate:dateConverted] autorelease]; 

      //Add the course to the to a temporary list to remove duplicated items    
      [tempResults addObject:course]; 
      course = nil; 
      [course release]; 
      [nameConverted release]; 
      nameConverted = nil; 
      [locationConverted release]; 
      locationConverted = nil; 
      [dateConverted release]; 
      dateConverted = nil; 
      NSLog(@"course retain count %i",[course retainCount]); 
      NSLog(@"name coverted retain count %i",[nameConverted retainCount]); 
      NSLog(@"location coverted retain count %i",[locationConverted retainCount]); 
      NSLog(@"date coverted retain count %i",[dateConverted retainCount]); 

的日誌告訴我,retainCount = 0;,所以我不明白爲什麼有內存泄漏。你們能給我一些建議嗎?

再次感謝。

+0

course = nil; [課程發佈]; 是泄漏。通過將它設置爲零,即使沒有釋放它也會丟失物體。只需使用[課程發佈]即可發佈。 – trapper

回答

2

你在每個循環都有泄漏。你只發布3個最後的NSString。每次你重新分配一個新的NSString給你的3個變量(nameConverted,locationConverted,dateConverted)時,你會忽略它們指向的NSString對象的引用。這意味着內存泄漏。當你離開While循環時,你只能釋放它們中的最後3個。

+2

@Deepak:讓他找到解決方案,plizz ;-) –

+0

+1對此評論... – Aravindhan

+0

謝謝。很高興你同意,SO不應該是一個複製/粘貼資源發現者... –

0

您是否嘗試過使用:

nameConverted = [[NSString initWithUTF8String:name] autorelease]; 
locationConverted = [[NSString initWithUTF8String:location] autorelease]; 
dateConverted = [[NSString initWithUTF8String:date] autorelease]; 

和拆卸循環之外的版本? 也不要autoremove課程對象,將其添加到tempResults後釋放它。

+0

我已經嘗試過,但沒有奏效。 – madcoderz

+0

嘗試使用NSString * nameConverted = ....這些變量是循環的臨時對象嗎?只是autorelease他們在同一行 – Panagiotis

+0

這是我第一次嘗試,但謝謝。 – madcoderz

1

您正在每個循環創建一個內存塊,然後只釋放一次。因此,如果while循環運行10次以上,每個字符串的保留計數爲10,但只釋放一次。

要解決,把你的3個版本while循環裏面是這樣的:

if(sqlite3_prepare_v2([[DatabaseController sharedDatabaseController] getDb], sqlQueryConverted, -1, &dbStatement, NULL)==SQLITE_OK){ 

     //Run the query 
     while (sqlite3_step(dbStatement) == SQLITE_ROW) 
     {  


      const char *name = (const char *)sqlite3_column_text(dbStatement, 0); 
      int courseId = sqlite3_column_int(dbStatement, 1); 
      const char *location = (const char *)sqlite3_column_text(dbStatement, 2); 
      const char *date = (const char *)sqlite3_column_text(dbStatement, 3); 

      //Convert the returnedElement char to string 
      nameConverted = [[NSString alloc] initWithUTF8String:name]; 
      locationConverted = [[NSString alloc] initWithUTF8String:location]; 
      dateConverted = [[NSString alloc] initWithUTF8String:date]; 

      Course *course = [[[Course alloc]initWithName:nameConverted _id:courseId location:locationConverted courseDate:dateConverted] autorelease]; 

      //Add the course to the to a temporary list to remove duplicated items    
      [tempResults addObject:course]; 

     [nameConverted release]; 
     [locationConverted release]; 
     [dateConverted release]; 

     } 

    } 
+0

正如我之前所說的,如果我在while循環中放置3個版本,應用程序會崩潰。這就是爲什麼我卡住了。 – madcoderz

+0

對不起...錯過了那部分! – Louie

0

這就是你應該做的。

NSString *nameConverted = [[NSString alloc] initWithUTF8String:name]; 
NSString *locationConverted = [[NSString alloc] initWithUTF8String:location]; 
NSString *dateConverted = [[NSString alloc] initWithUTF8String:date]; 

Course *course = [[Course alloc] initWithName:nameConverted _id:courseId location:locationConverted courseDate:dateConverted]; 
[tempResults addObject:course]; 
[course release]; 

[dateConverted release]; 
[locationConverted release]; 
[nameConverted release];