2012-05-07 40 views
0

我的iOS App中有内存管理问题。我加载一个XML并将这个XML中的所有值设置为Spezific Objects。现在我的问题是,当我重新加载XML每15-20重新加载这个XML我的应用程序崩溃解析这里是我的解析器的一个样本。iOS编程中的内存管理

编辑:这里北京时间当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]; 

}

+0

什么显示的错误,什么线?你知道吗? – Saad

+0

这里IST的错误,当我启用NSZombie - [CFNumber保留]:发送到释放实例 – Zeropointer

+1

消息只需使用ARC,这将解决所有的你的问题! –

回答

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]; 
    } 


    } 
+0

autorelease不会工作 – Zeropointer

+0

是的,这是由于relaesed对象。如果你有nsmutable数组,然后使用 [NSMutablArray arra];并取出呼吁释放,这将返回UA保留对象或可变数组 – Saad

+0

是啊,我知道了,你还没有分配localtempObjects和释放它,看到编辑答案 – Saad