我有一个关于使用 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