当函数使用getter时,Objective C中的EXC_BAD_EXCESS

时间:2012-02-11 23:57:11

标签: objective-c nsdictionary exc-bad-access

- (NSDictionary *)myPropertyList   //Calling getter of myPropertyList
{
    if(!_myPropertyList){
        NSArray *colorsUsed = [[NSArray alloc] initWithObjects:[UIColor redColor], [UIColor blueColor], nil];
        _myPropertyList=[[NSDictionary alloc] initWithObjectsAndKeys: colorsUsed, @"colorUsed",[NSNumber numberWithDouble:MAX_RADIUS],@"maximumRadius",[NSNumber numberWithDouble:MIN_RADIUS],@"minimumRadius", [NSNumber numberWithInt:20],@"circleNumbers", nil];;
    }
    return _myPropertyList;
}

- (NSArray *)generateCircleList
{
    int minRadius=[[self.myPropertyList objectForKey:@"minimumRadius"] intValue];
    int maxRadius=[[self.myPropertyList objectForKey:@"maximumRadius"] intValue];
    ...
    //code continues

我的模型中有这段代码。当调用使用myPropertyList的函数generateCircleLlist时,它会因EXC_BAD_ACCESS错误而崩溃。我真的环顾四周,但无法找到解决方案。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

您正在创建colorsUsed但使用colorUsed,还添加[colorsUsed release];用于内存管理。

if(!_myPropertyList){

    NSArray *colorsUsed = [[NSArray alloc] initWithObjects:[UIColor redColor], [UIColor blueColor], nil];
    _myPropertyList=[[NSDictionary alloc] initWithObjectsAndKeys: colorsUsed, @"colorUsed",[NSNumber numberWithDouble:MAX_RADIUS],@"maximumRadius",[NSNumber numberWithDouble:MIN_RADIUS],@"minimumRadius", [NSNumber numberWithInt:20],@"circleNumbers", nil];

    [colorsUsed release];
}