参考までに、私はすでに読んだ:
私はそれが役立つと思いました:)。
このアプリは教育ツールであり、人々が単純な遺伝学を視覚化するのを助けることを目的としています。変数名などが意味をなすように、いくつかの背景があります。アプリの実行時に実行されるメインコードは次のとおりです。
- (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];
}
そして、これをさまざまな方法で実行するとどうなりますか?
- ブレークポイントなしで実行すると、2番目のsleep()の後、「ラウンド3」NSLogの前に、コンソールメッセージなしでクラッシュします。
- ブレークポイントを有効にして実行しますが、定義されていない場合は、シーケンス全体を実行します。4番目のsleep()の後、EXC_BAD_ACCESSでクラッシュします。
- ブレークポイントを有効にしてNSZombiesEnabledで実行すると、上記と同じことを実行します。これ以上の情報はなく、EXC_BAD_ACCESSだけです。
- Instrumentsで実行している場合、リークは表示されません。
これは、4回呼び出されるルーチンです。
-(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];
}
コード全体を貼り付けないと、possiblesByMatingFather: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];
これはほとんどの場合うまくいくようです。少なくとも、ブレークポイントが有効になっている場合、プログラムは、上記のように、最後まで文句なしにコードを実行します。
ここで、私が間違っていることについて何か提案はありますか?明らかにある種のメモリ管理の問題ですが、頭の中で分類することはできません。
ところで、無駄に、壁に物を投げてそれを理解しようとして、私はメインルーチンから1行を次のように変更しました:
// get possibilities
NSArray *possibilities = [[self possibilitiesByMatingFather:father
withMother:mother
mutations:isMutation] retain];
無駄に。同じ結果。したがって、問題は、possiblesByMatingFather:withMother:mutationsによって返される配列を保持することではありません。そのリターンを保持することを強制することは役に立ちません。