0

我的 iOS 应用程序中的内存管理有一点问题。我加载一个 XML 并将这个 XML 中的所有值设置为特定对象。现在我的问题是,当我每 15-20 次重新加载此 XML 重新加载 XML 时,我的应用程序 Crash on Parsing 是我的解析器的一个示例。

编辑:这里是 NSZombie 启用时的错误 如果 NSZombie 被禁用我没有收到错误消息。-[CFNumber retain]:消息发送到释放的实例

感谢帮助。

编辑:

我的代码的开头:

 - (id)init
{
    self = [super init];
    if (self) {
        TheObjects *theObjects = [[TheObjects alloc] init];

        [self parse];
    }
    return self;
}

- (void) reload{
    reload = YES;
    TheObjects *theTmpObjects = [[TheObjects alloc] init];
     [self parse];
}
- (void)parse{

for (id xmlOBject in xmlObjects){
    MyObject *object = [[MyObject alloc] init];
    object.number1 = [NSNumber numberWithInt:1];
    object.number2 = [NSNumber numberWithInt:2];
    object.number3 = [NSNumber numberWithInt:3];

    if (reload)
        [theTmpObjects.objects addObject:object];
    else 
        [theObjects.objects addObject:object];

    [object release];
}
//later in my code

TheObjects *localTheTmpObjects = nil;
if (reload){
    localTheTmpObjects = theObjects;
    theObjects = theTmpObjects;
}

if ([delegate respondsToSelector:@selector(finished:)]){
    [delegate performSelector:@selector(finished:) withObject:theObjects];
}

if(reload)
    [localTheTmpObjects release];

}

4

1 回答 1

1
remove the line [localTheTmpObjects release]

you don't own the object

at the end, call the  `[localTheTmpObjects autorelease];`
this is because if you release array, all its objects are released and hence may cause crash, when your array may in use

    - (id)init
    {
        self = [super init];
        if (self) {
            TheObjects *obbjects = [[TheObjects alloc] init];
    theObjects = objects;
[objects releas];
            [self parse];
        }
        return self;
    }

    - (void) reload{
        reload = YES;
TheObjects *obbjects = [[TheObjects alloc] init];
    thetmpObjects = objects;
[objects releas];         [self parse];
    }
    - (void)parse{

    for (id xmlOBject in xmlObjects){
        MyObject *object = [[MyObject alloc] init];
        object.number1 = [NSNumber numberWithInt:1];
        object.number2 = [NSNumber numberWithInt:2];
        object.number3 = [NSNumber numberWithInt:3];

        if (reload)
            [theTmpObjects.objects addObject:object];
        else 
            [theObjects.objects addObject:object];

        [object release];
    }
    //later in my code

    TheObjects *localTheTmpObjects = nil;
    if (reload){
        localTheTmpObjects = theObjects;
        theObjects = theTmpObjects;
    }

    if ([delegate respondsToSelector:@selector(finished:)]){
        [delegate performSelector:@selector(finished:) withObject:theObjects];
    }


    }
于 2012-05-07T15:10:13.497 回答