内存问题的代码如下:
拥有EXC_BAS_ACCESS
@interface MapList : NSObject
{
NSArray* m_units;
}
-(MapList*) loadMapListWithLevel:(uchar) lvl chapter: (uchar) chapt;
-(void) dealloc;
@end
和实施:
-(MapList*) loadMapListWithLevel:(uchar) lvl chapter: (uchar) chapt
{
self =[super init];
if (self)
{
{
...
NSMutableArray* array = [[NSMutableArray alloc] initWithCapacity:size];
for (uint j=0;j<size;j++)
{
obj =[[SpriteDB alloc] init];
[array addObject:obj];
[obj release];
}
if (i==0)
m_units = [NSArray arrayWithArray:array];
[array release];
}
...
}
return self;
}
-(void) dealloc
{
[m_units release];
[super dealloc];
}
在一种方法中,我称之为
MapList* mpl = [[MapList alloc] loadMapListWithLevel:level chapter:chapter];
[mpl release];
哪里有问题?当我评论[m_units release];
时,它有效......
答案 0 :(得分:2)
m_units = [NSArray arrayWithArray:array];
为您提供自动释放的值。当您执行第二个版本时,您将过度释放此对象。即使在runloop返回后访问此值也会给您带来内存问题。保留此值,并在dealloc上释放它。此外,您应该使用properties。