I have this situation:
- (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
In the beginning, I had a warning for otherClassString and for classString but with this way for otherClassString this work.
Now my problem is in classString in the MyObject. I tried a lot of things, but this warning is always shown. Can’t I call a class method inside a class method?
Your
+otherClassStringcreates an object with retain count 1 and returns it. This is used as return value for+classStringas well.If your methods don’t begin with
init,new, orcopy, you should return autoreleased objects. Everywhere where yours (as is) are used, they’ll be expected to return an autoreleased object.