这会造成任何泄漏吗?

Ile*_*ruz 1 cocoa memory-leaks objective-c nsarray

我有一个数组包含mutableCopy中Store类型的对象.我发布数组,在释放数组之前释放所有对象吗?

NSMutableArray *stores=[[NSMutableArray alloc]init];
[stores addObject:[store1 mutableCopy]];
[stores addObject:[store2 mutableCopy]];
[stores addObject:[store3 mutableCopy]];
...
[stores release];
Run Code Online (Sandbox Code Playgroud)

Gui*_*ume 6

如果不使用ARC,则需要在某个时刻释放对象,因为该- mutableCopy方法会创建一个保留计数为+1的新对象.为避免泄漏,您应该写:

[stores addObject:[[store1 mutableCopy] autorelease];
Run Code Online (Sandbox Code Playgroud)

现在,没有理由不使用ARC.如果你使用ARC,你没事,你也不会打电话- release.