2012-05-07 37 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