Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to release static Objective-C variables

The StackOverflow question "using static keyword in objective-c when defining a cached variable" references code from Example 4 of Xcode's TableViewSuite that defines a static NSDateFormatter and calls alloc but never calls release.

Shouldn't static variables be released? If yes, where in the code should they be released? If no, why not?

like image 920
ma11hew28 Avatar asked Apr 06 '11 15:04

ma11hew28


1 Answers

Shouldn't static variables be released? If yes, where in the code should they be released? If no, why not?

It depends. If the variable is initialized only once, and should stay around for the lifetime of the application, then no, it shouldn't be released (its memory will essentially be freed when the application exits, anyway). If, however, the value of the static variable changes, then yes, the previous object should be released when the static variable is set to a new object.

like image 182
mipadi Avatar answered Oct 09 '22 13:10

mipadi