A simplified version of Jonathan Grynspan's accepted answer:
The retain
isn't for the variable which points to an object. That variable will last forever because it's static. The retain
is for the object the variable points to. Without the retain
the object could (and should) be deallocated. Then you've got a variable pointing to a thing which will cause a sigabrt
. This variable pointing nowhere is known as a "dangling pointer."
For the ARC context, the best thing to do is declare the static variable as strong, so something like this:
static ThatClass * __strong thatStaticVariable;
This ensures that the object that thatStaticVariable
points to will be a valid object (i.e., never gets deallocated) once assigned. However, you don't actually need the __strong keyword at all, because it's the default (so sayeth the docs, thanks to @zpasternack), so just use
static ThatClass *thatStaticVariable;
and you're good.
Note: forever = while the application is running