I have a quite simple question about memory management in Objective-c and methods calling.
Imagine I have :
- (void)someFunction
{
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"Value 1", nil];
[self someOtherFunction:array];
}
- (void)someOtherFunction:(NSMutableArray *)array
{
// Should I retain array here?
[array addObject:@"Value 2"];
// And then release ?
}
This is a simple exemple but imagine we have like 10 method calls with the same object parameter.
What’s the best solution ?
If you’re being all belt-and-braces or are doing lots of odd things with threads then you should probably retain/release inside each method call. In fact, this is exactly what ARC does for you behind the scenes. (It’s not documented as far as I know and may change from version to version.)
And, certainly, it won’t do any harm to retain/release as you suggest. In practice it’s unlikely to add much of an overhead.
Having said all that: most people don’t add the retain/release. If your code is all running on the main thread it’s very unlikely that your object will be released while you’re executing your method.