0

iOS アプリのメモリ管理に少し問題があります。XML をロードし、この XML のすべての値を特定のオブジェクトに設定します。今、私の問題は、この XML の 15 ~ 20 回のリロードごとに XML をリロードするときです。解析時にアプリがクラッシュします。これはパーサーのサンプルです。

編集:NSZombieが無効になっている場合、NSZombieが有効になっている場合のエラーは次のとおりです。エラーメッセージは表示されませんでした。-[CFNumber 保持]: 割り当て解除されたインスタンスに送信されたメッセージ

手伝ってくれてありがとう。

編集:

私のコードの冒頭:

 - (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 に答える