Rod*_*igo 2 memory-leaks class objective-c class-method
我有这种情况:
- (void) foo {
NSLog(@"Print this: %@", [MyObject classString]);
}
// So in MyObject.m I do
@implementation MyObject
+ (NSString *) classString {
return [OtherObject otherClassString]; //The Warning "Potential leak..." is for this line
}
@end
// Finally in OtherObject
@implementation OtherObject
+ (NSString *) otherClassString {
NSString *result = [[NSString alloc] initWithString:@"Hello World"];
return result;
}
@end
Run Code Online (Sandbox Code Playgroud)
一开始,我对这项工作有一个警告otherClassString
,classString
但是otherClassString
这样做.
现在我的问题是classString
在MyObject
.我尝试了很多东西,但这个警告总是显示出来.我不能在类方法中调用类方法吗?
您+otherClassString
创建一个保留计数为1的对象并将其返回.这也用作返回值+classString
.
如果你的方法不开始init
,new
或者copy
,你应该返回自动释放的对象.在使用你的所有地方(按原样),他们都会被要求返回一个自动释放的对象.
+ (NSString *) otherClassString {
NSString *result = [[[NSString alloc]
initWithString:@"Hello World"]
autorelease];
return result;
}
Run Code Online (Sandbox Code Playgroud)