【发布时间】:2010-10-05 17:04:04
【问题描述】:
供参考,我已经阅读:
- memory management question -- releasing an object which has to be returned
- iPhone: Return NSMutableArray in method while still releasing
- Releasing objects returned by method
我认为这会有所帮助:)。
这个应用程序是一个教学工具,旨在帮助人们想象简单的遗传学。只是一些背景,所以变量名称和东西会有意义。以下是应用运行时执行的主要代码:
- (void)viewDidAppear:(BOOL)animated {
ThingzCore *core = [[ThingzCore alloc] init];
ThingzThing *thing1 = [[ThingzThing alloc] init];
thing1.breed = @"AB";
thing1.pattern = @"AC";
thing1.color = @"CA";
thing1.gender = @"XY";
thing1.generation = 1;
thing1.isEgg = NO;
ThingzThing *thing2 = [[ThingzThing alloc] init];
thing2.breed = @"CD";
thing2.pattern = @"BA";
thing2.color = @"CB";
thing2.gender = @"XX";
thing2.generation = 1;
thing2.isEgg = NO;
NSLog(@"Breeding GD BR PT CL G");
ThingzThing *child = [core mateFather:thing1 withMother:thing2];
NSLog(@"Round 1: %@ %@ %@ %@ %d",child.gender,child.breed,child.pattern,child.color,child.generation);
sleep(10);
child = [core mateFather:thing1 withMother:thing2];
NSLog(@"Round 2: %@ %@ %@ %@ %d",child.gender,child.breed,child.pattern,child.color,child.generation);
sleep(10);
child = [core mateFather:thing1 withMother:thing2];
NSLog(@"Round 3: %@ %@ %@ %@ %d",child.gender,child.breed,child.pattern,child.color,child.generation);
sleep(10);
child = [core mateFather:thing1 withMother:thing2];
NSLog(@"Round 4: %@ %@ %@ %@ %d",child.gender,child.breed,child.pattern,child.color,child.generation);
sleep(10);
[thing1 release];
[thing2 release];
[core release];
}
当我以各种方式运行它时会发生以下情况:
- 在没有断点的情况下运行,它在第二次 sleep() 之后但在“第 3 轮”NSLog 之前崩溃,没有控制台消息。
- 在启用断点但未定义断点的情况下运行,它将贯穿整个序列。在第四次 sleep() 之后,它会因 EXC_BAD_ACCESS 而崩溃。
- 在启用断点和 NSZombiesEnabled 的情况下运行,它的作用与上述相同 - 没有更多信息,只是 EXC_BAD_ACCESS。
- 在 Instruments 中运行,未显示泄漏。
这是被调用四次的例程:
-(ThingzThing *)mateFather:(ThingzThing *)father
withMother:(ThingzThing *)mother {
// will we be doing a mutation?
int mutationPercentage = father.generation + mother.generation;
int mutationNumber = (arc4random() % ((unsigned)100 + 1));
BOOL isMutation = NO;
if (mutationNumber <= mutationPercentage) {
isMutation = YES;
}
// get possibilities
NSArray *possibilities = [self possibilitiesByMatingFather:father
withMother:mother
mutations:isMutation];
// randomly select one of the possibilities
int keeping = (arc4random() % ((unsigned)[possibilities count]));
return [possibilities objectAtIndex:keeping];
}
如果不粘贴整个代码,可能性ByMatingFather:withMother:mutations 函数将返回一个 NSMutableArray。该例程使用以下方法声明数组:
NSMutableArray *possibilities = [NSMutableArray array];
然后:
return possibilities;
它不会向可能性发送释放或自动释放消息;我的理解是,以我的方式创建数组是一种隐式自动释放。我不想分配数组,因为我正在返回它,所以没有机会显式释放它。
NSMutableArray 中保存的对象是一个自定义类。添加如下:
ThingzThing *newThing = [[ThingzThing alloc] init];
newThing.breed = choiceBreed;
newThing.gender = choiceGender;
newThing.color = choiceColor;
newThing.pattern = choicePattern;
newThing.generation = mother.generation + father.generation;
newThing.name = @"";
newThing.isEgg = YES;
[possibilities addObject:newThing];
[newThing release];
这似乎在大多数情况下都有效。至少,当断点被启用时,程序会毫无怨言地运行代码直到结束,如上所述。
关于我做错了什么的任何建议,在这里?这显然是某种内存管理问题,但我无法在脑海中对其进行分类。
顺便说一句,徒劳无功地试图弄清楚,我确实修改了主程序中的一行如下:
// get possibilities
NSArray *possibilities = [[self possibilitiesByMatingFather:father
withMother:mother
mutations:isMutation] retain];
无济于事。结果相同。所以问题不在于保留可能性ByMatingFather:withMother:mutations 返回的数组。强制保留该回报并没有帮助。
【问题讨论】:
-
我知道这是一个愚蠢的问题,但可能会导致错误。你能删除所有
sleep()行吗?嗯...只是尝试,因为我在您的代码中看不到任何错误:) -
第 4 次睡眠后它会中断吗?离开此方法后是否可能引发异常?请在此方法的最后一行添加一些 NSLog-Foo
-
尝试将 [NSMutableArray array] 替换为 [[[NSMutableArray alloc] init] autorelease],看看会发生什么...同时发布可能性ByMatingFather 方法
-
移除了 sleep();不用找了。在 viewDidAppear 方法的最后添加了一个 NSLog,并得到了日志输出,但仍然得到了 EXC_BAD_ACCESS。请参阅下面对威廉建议的评论。
标签: iphone autorelease