2011-01-27 114 views
1

相當與Objective-C的新的,但設法到目前爲止從頭創建一個卡的應用程序。 遊戲的思想是針對PC玩一個紙牌遊戲。消息發送到釋放實例

在功能updatePlayerCardView我拿到小組第一的玩家已經卡和地方按鈕同組的名稱在屏幕上。 這一次工作得很好,但是當函數被調用第二次就失敗:

- [CFString字符串挽留]:發送到釋放實例0x604ac60

一些調試和研究後的消息我計算過,在發生這種情況的行是:

[cardButton的setTitle:[的NSString stringWithFormat:@ 「%@(%I)」,[基組名],[播放器getCardGroupCount:組]] forState:UIControlStateNormal];

所以NSString的被釋放明顯,事情是我不明白的是,所有按鈕都應該重新創建,那麼爲什麼我要有一個釋放的實例? 是否有更好的方式來每次更新播放器視圖。從一開始就不知道按鈕的數量。

-(void)updatePlayerCardView { 

NSMutableArray *groups = [[[game players] objectAtIndex:0] getCardGroups]; 
Player *player = [game getPlayer:0]; 
int x = 13; 
int y = 157; 

for(int i = 0; i < [groups count]; i++) { 
    QuartetGroup *group = [groups objectAtIndex:i]; 
    int cardGroupCount = [player getCardGroupCount:group]; 

    if(cardGroupCount == [[game quartet] getCardsPerGroupCount]) { 

    }else{ 
     UIButton *cardButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     cardButton.frame = CGRectMake(x, y, 140, 37); 
     cardButton.tag = i; 
     [cardButton setTitle:[NSString stringWithFormat:@"%@ (%i)", [group groupName],[player getCardGroupCount:group]] 
        forState:UIControlStateNormal]; 
     [cardButton addTarget:self action:@selector(playerCardClicked:) forControlEvents:UIControlEventTouchUpInside]; 
     [[self view] addSubview:cardButton]; 
     //NSLog(@"Card group added to view %@", [group groupName]); 
     if(i == 2 || i == 5 || i == 8 || i == 11) { 
       x += 157; 
       y = 157; 
     }else{ 
       y += 45; 
     } 
     } 
    } 
} 

回答

2

看起來像groupName已被釋放。它可能發生在兩種情況:

  • 你忘了保留它的地方。你確定它保留了group
  • 你的某處釋放了,你不應該。

我可以建議你兩種方法如何處理這個問題:

  • 仔細閱讀你的代碼,以確保您妥善管理groupName壽命。
  • 您可以創建MyString類,從NSString繼承它並聲明groupNameMyString。然後,您可以將斷點放入dealloc方法中,並查找何時解除分配。它可以幫助您找出groupName生命週期的錯誤。
+0

groupName在類QuartetGroup中設置: – Johan 2011-01-27 15:11:17

1

首先,不要命名方法get*get在Cocoa/iOS中有一個非常具體的含義,並且通用的getter不是它。

接下來,你用你的代碼「的建立與分析」?修復它識別的所有問題。

最後,發佈崩潰發生的位置的回溯。此外,發佈所有與設置和管理過度釋放對象相關的代碼。

相關問題