0

私のアプリでは、ユーザーは表ビュー内に回答を入力して、特定の質問に答える必要があります。ユーザーがデータを入力する 1 つのカスタム セルで始まるテーブル ビューがあります。そのセルには、ユーザーがさらにデータを入力する別のカスタム セルを読み込むことができるボタンがあります。

ユーザーが質問に答えると、テーブル ビューが変更され、新しい質問の最初のセルのみが表示されます。leftData.equationまた、ユーザーの入力の追跡を担当するものを含む、いくつかの「データ」配列は、すべてのオブジェクトからクリアされます。( [array removeAllObjects])

2 番目のセルがロードされたときも、次の質問のために最初のセルに戻ったときも、クラッシュはありません。しかし、2 番目のセルを再度ロードしようとすると、プログラムが Bad Access でクラッシュします。配列と関係があると思いますが、leftData.equation

以下は、2 番目のセルが読み込まれたときに呼び出されるメソッドです。

- (void) setUpBalanceCell: (UITableView *) tableView  {

    balanceCell = (BalanceCell *) [tableView dequeueReusableCellWithIdentifier:@"balanceCell"];

    if (balanceCell == nil) {   

        NSArray *nib =   [[NSBundle mainBundle] loadNibNamed:@"BalanceCell" owner:self options:nil];
        balanceCell = (BalanceCell*) [nib objectAtIndex:0];
        [balanceCell.rightButton addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];

    }
        // stores data from first cell into a data array
        for (FormulaLabel *label in equationCell.leftView.equationOrder) 
            [leftData.equation addObject:label.text];   //fix:  needs to comply with MVC   

        for (FormulaLabel *label in equationCell.rightView.equationOrder)  
            [rightData.equation addObject:label.text];  

        [leftData setUpBalancedEquation];
        [rightData setUpBalancedEquation];

        [self setUpView:balanceCell.leftView fromArray:leftData.equation toArray:leftBalanceItems]; // WHERE EXC_BAD ACCESS appears
        [self setUpView:balanceCell.rightView fromArray:rightData.equation toArray:rightBalanceItems];

}

アクセス不良の呼び出し方

- (void)setUpView:(UIView *)view fromArray:(NSMutableArray *)equationData toArray:(NSMutableArray *)balanceItems {

int labelCount = 0; //label #
int startingPoint = 5; //x vaiue where first label starts

//Crashes in method below
for (NSString *equationText in equationData) {

    //add text
    FormulaLabel *tempLabel = [[FormulaLabel alloc] initWithFrame:CGRectMake(startingPoint, 2, 10, 22)];
    tempLabel.text = equationText;
    [tempLabel sizeToFit];
    [view addSubview:tempLabel];
    tempLabel.tag = labelCount;
    [balanceItems addObject:tempLabel];
    //set location of '+'
    startingPoint = tempLabel.frame.origin.x + tempLabel.frame.size.width + 3;

    if (labelCount != [equationData count]-1) { //not the last label

        UILabel *plus = [[[UILabel alloc] initWithFrame:CGRectMake(startingPoint, 5, 10, 10)]autorelease];
        plus.text = @"+";
        plus.font = [UIFont systemFontOfSize:13];
        [plus sizeToFit];
        [view addSubview:plus];
        startingPoint = plus.frame.origin.x + plus.frame.size.width + 3;
        [balanceItems addObject:plus];
    }

    labelCount ++;
    [tempLabel release];
}

}

4

1 に答える 1

1

まあ、それを保持することで間違いなく修正されますが、もし私があなただったら、そもそもアレイがどこで解放されているかを調べます。

を呼び出すretainと、オブジェクトの保持カウントがインクリメントされます。最初にオブジェクトを作成すると[NSObject alloc]、保持カウントが 1 に設定されます。呼び出すとretainカウントが増加するため、2 になります。release一方、を呼び出すと、保持カウントが 1 減少します。したがって、release保持カウントが のオブジェクトを呼び出すとします。 1 の場合は 0 になります。0 の場合、オブジェクトはメモリから解放されます (そして が呼び出されますdealloc)。

これで物事が明確になることを願っています。メモリ管理は非常に紛らわしいです。プロジェクトの後半で、メモリ リークのテストを検討することをお勧めします。

幸運を祈ります。

于 2012-07-02T05:25:55.583 に答える