我正在使用 XCode 开发 iPhone 应用程序。我是这个平台的新手,需要一些关于特定问题的帮助......
我有一个方法可以处理一些数据并返回两个整数值作为包裹到 NSMutableArray 中的 NSNumber。
这是方法:
-(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];
}
我从另一种方法调用上述方法,将 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'
}
你能解释一下我在内存释放过程中哪里出错了。
我对这个过程的理解有点不稳定。请帮忙。