How to release static Objective-C variables

非 Y 不嫁゛ 提交于 2019-11-30 08:28:14

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.

paracycle

As the accepted answer to that question already states, releasing static variables is impossible. They act like global variables that are only visible to your function with a lifetime as long as your program.

If you are concerned about bloat because of variables that that static variable holds on to, then you should (somehow) release those references. Thus, for example, if your static variable is an NSMutableArray, and you keep adding objects inside, it will always keep growing, unless you, at some point, empty the array.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!