I have been searching through the web and reading many posts, but none has yet to explain me how this exactly works. I know that the convention says that any method must return an autoreleased object (except for alloc, new, copy and mutableCopy) so something like this is fine:
-(MyClass*)findRandomName { return [[[MyClass alloc] initWithString:@"Something"] autorelease]; }
My question is, should I also call autorelease on the return value if I use a convenience method, or something similar? like here:
-(MyClass*)findRandomName { return [[MyClass startFromString:@"Something"] autorelease]; }
And why, or why not ? Thanks in advanced.
Your first example is incorrect as it should call
[MyClass alloc]before callinginitWithString, e.g.:In the second example you should not
autoreleasethe object. ThestartFromStringmethod should alreadyautoreleasethe object and if youautoreleaseit again you will over release the object.In the second example the
findRandomNamemethod does not take ownership of the object (because the method it calls does not containalloc,new,copyormutableCopyin the name) so it should notreleasethe object.Your statement that “the convention says that any method must return an autoreleased object (except for init, new and copy)” is not quite correct. The memory management policy says: