Case 1:
-(id)getAnObject{
Object *someObject = [[Object alloc] init];
//doing something
return someObject;
}
Case 2:
-(void)dealWithAnObject{
Object *someObject = [[Object alloc] init];
[assignTheObjectToOther someObject];
}
Both Case 1 and Case 2 have some warning in XCode, what should I do to deal with these two? thank you.
The golden rule of memory management is: everything needs to know which objects it owns. And only you can make that decision.
I highly recommend reading Apple’s memory management guide. At least twice.
For your specific case:
getAnObjectdoesn’t ever release the object it creates. If you’re going to return it, you’ll want toautoreleaseit first.dealWithAnObjectalso doesn’t release its object. You can eitherautoreleaseit orreleaseit after the function call that uses it. Make sure that anything that uses it follows the same rules, and you’ll be OK.