作为参考,我已经阅读:
我认为这会有所帮助:)。
这个应用程序是一个教学工具,旨在帮助人们想象简单的遗传学。只是一些背景,所以变量名称和东西会有意义。这是应用程序运行时执行的主要代码:
- (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 返回的数组。强制保留该回报并没有帮助。