NSString の使用に関するメモリ管理の質問があります。以下の簡単なコードを見てください。
NSString *catSeq = [[NSString alloc] init];
for (NSString *cat in cats) {
catSeq = [catSeq stringByAppendingFormat:cat];
catSeq = [catSeq stringByAppendingFormat:@" "];
}
catLabel.text = catSeq;
[catSeq release];
// This code expresses what all I want to do, but Memory leak happened
// below I delete lines of code to find out the reason
-----------------------------------------------------
NSString *catSeq = [NSString string];
for (NSString *cat in cats) {
catSeq = [catSeq stringByAppendingFormat:cat];
catSeq = [catSeq stringByAppendingFormat:@" "];
}
catLabel.text = catSeq;
// Memory leak not happened
-----------------------------------------------------
NSString *catSeq = [[NSString alloc] init];
catLabel.text = catSeq;
[catSeq release];
// Memory leak not happened