2011-04-11 81 views
2

我得到這個問題可以任何人幫我 這裏是我的代碼。此代碼工作第一次點擊,但是當點擊2次就得到錯誤內存問題與陣列釋放在objective-c

的malloc:錯誤對象0x4e226a4:用於釋放對象不正確的校驗 - 對象是在被釋放後,可能被修改。 * *設置malloc_error_break斷點去**錯誤

- (void)updateTextViewContents { 
    content = [[NSMutableString alloc] init]; 
    for (int i = 0; i <[ _scoresArray count]; i++) 
    { 
    NSMutableString *data = [_scoresArray objectAtIndex:i]; 
     [content appendString:data]; 
     if([content isEqualToString:UserText.text]&&[content isEqualToString:PassText.text]) 
     { 
     UIAlertView *alt = [[UIAlertView alloc] initWithTitle:nil message:@"Valid User" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
      [alt show]; 
      [alt release]; 
      [content release]; 

     } 
     else  
     { 
     UIAlertView *alt1 = [[UIAlertView alloc] initWithTitle:nil message:@"NOT A Valid User" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alt1 show]; 
     [alt1 release]; 
     } 
    } 

當我鬆開了這裏工作,但是當我輸入正確的用戶名和密碼,點擊第二次都改變視圖顯示在時間。我認爲它會發生,因爲數組獲得添加在每一個cilck這就是爲什麼兩個視圖顯示時間如何我這樣解決。 // [內容發佈];

}

回答

0

這裏產生,因爲,

內存問題你如果條件釋放內容的NSMutableString,然後再追加吧。

所以,不要釋放for循環,釋放它for循環。

- (無效)updateTextViewContents {

content = [[NSMutableString alloc] init]; 

for (int i = 0; i <[ _scoresArray count]; i++) 
{ 

    NSMutableString *data = [_scoresArray objectAtIndex:i]; 
    [content appendString:data]; 
    if([content isEqualToString:UserText.text]&&[content isEqualToString:PassText.text]) 
    { 
     UIAlertView *alt = [[UIAlertView alloc] initWithTitle:nil message:@"Valid User" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alt show]; 
     [alt release]; 
     //[content release]; - Not release here. 

    } 
    else  
    { 
     UIAlertView *alt1 = [[UIAlertView alloc] initWithTitle:nil message:@"NOT A Valid User" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alt1 show]; 
     [alt1 release]; 
    } 
} 

[content release]; 
+0

它的工作,但犯規給我看別的警報視圖當我輸入錯誤的用戶名和密碼 – Harish 2011-04-11 07:40:05

+0

你好,請從內容變量,而不是釋放它的字符串。要刪除字符串,你可以使用[content deleteCharactersInRange:((NSRange)range)];方法。 – 2011-04-11 07:49:13

0

你能不能用這個

[content appendString:[_scoresArray objectAtIndex:i]]; 
0

你既警報顯示,因爲你的循環放break後更換

NSMutableString *data = [_scoresArray objectAtIndex:i]; 
     [content appendString:data]; 

是有效的用戶,因爲你肯定是沒有經過循環後,它是有效的用戶 希望這會有所幫助 好運

0

您在if條件中有版本內容。對於下一次迭代,您將再次嘗試將數據附加到內容,如果在以前的迭代中條件爲真,則已經釋放內容。因此你得到那個錯誤object was probably modified after being freed