我正在使用XCode開發iPhone應用程序。我是這個平臺的新手,需要一些特定的問題的幫助...NSMutableArray發佈導致崩潰
我有一個方法,處理一些數據並返回兩個整數值作爲NSNumber包裝成一個NSMutableArray。
這裏是方法:
-(NSMutableArray *)processPoints:(int) x:(int) y
{
NSMutableArray *mutArray = [[NSMutableArray alloc] initWithCapacity:3];
int x9,y9;
// ...do some processing...
NSNumber* xNum = [NSNumber numberWithInt:x9];
NSNumber* yNum = [NSNumber numberWithInt:y9];
[mutArray addObject:xNum];
[mutArray addObject:yNum];
return [mutArray autorelease];
}
我調用上述方法從另一種方法,其中I的NSNumber的東西複製到局部變量,然後釋放NSMutable陣列的本地副本。
但是,當釋放這個NSMutable數組(變量'mutArray')時,應用程序崩潰。
這裏是方法:
-(void)doNinjaAction
{
NSMutableArray* mutArray = [self processPoints: x :y];
NSNumber* s1 = [[mutArray objectAtIndex:0] retain];
NSNumber* s2 = [[mutArray objectAtIndex:1] retain];
x = [s1 integerValue];
y = [s2 integerValue];
//...proceed with other stuff...
[mutArray autorelease]; //this is where the system crashes. same for 'release'
//instead of 'autorelease'
}
能否請你解釋一下,我正與內存釋放的過程中走錯了。
我對這個過程的理解有點不穩定。請幫忙。
它正在崩潰,因爲你自動釋放它兩次。 – 2013-02-12 06:32:34