I'm evaluating the switch to ARC (automatic reference counting) and the refactoring to apply to my code. Among the things I have to figure out, there is this:
what should I do in didReceiveMemoryWarning if the explicit release of objects is not allowed by ARC? Currently, I use that method to release objects belonging to my controller and that are easily retrievable via lazy getters:
- (void)didReceiveMemoryWarning {
[_foo release]; _foo = nil;
[super didReceiveMemoryWarning];
}
and the relative lazy getter:
- (Foo *)foo {
if (_foo) {
return _foo;
}
return (_foo = [[Foo alloc] init]);
}
It seems impossible to implement such "pattern" in ARC… so, what should I do? Should didReceiveMemoryWarning be considered "deprecated"/useless in ARC?
ARC handles the retain and release code so setting _foo to be nil is sufficient to allow the ARC generated code to perform the release. You don't explicitly release, you simply manage your object graph and the ARC generated code will perform the release when appropriate.
Read Apple's Transitioning To ARC Release Notes document for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With