Memory Management in Objective C regarding reference count
NOT USING ARC OR AUTOMATIC GARBAGE COLLECTION
-(Fraction*) add:(Fraction*) f
{
Fraction*result =[[Fraction alloc] init];//To do
return result;
}
//after the function returns. What will the reference count of object that result was pointing to.
See main.m below.
IN main.m
int main(int argc,char* argv[])
{ //To do
Fraction*ans =[[Fraction alloc] init];
ans=[f1 add: f2];
//I guess that the refrence count of the object that ans will be pointing
after this statement will be 1 or 2.//...
}
An extract regarding this from stephen kochan objective-c
When using manual memory management, this method presents a problem. The result object is alloc’ed and returned from the method after the calculations have been performed. Because the method has to return that object, it can’t release it—that would cause it be destroyed right then and there. Probably the best way to resolve this issue is to autorelease the object so that its value can be returned, with its release delayed until the autorelease pool is drained. You can take advantage of the fact that the autorelease method returns its receiver and embeds it in expressions like this:
Fraction*result =[[[Fraction alloc] init] autorelease];
//or like this:
return[result autorelease];
iOS 7, reference count