单例创建对象一次,会在App被退出时被系统回收,不用担心内存泄漏的。
官方文档中是这么解释的:
" When an application terminates, objects may not be sent a dealloc message since the process’s memory is automatically cleared on exit—it is more efficient simply to allow the operating system to clean up resources than to invoke all the memory management methods."
大致的意思就是:当应用程序退出时,对象不会接受到一个dealloc消息,系统会自己清理所有的资源,Apple认为这样比去调用一个内存管理方法更有效率.
Cocoa With Love的Matt关于我提问的解答是这样的:
"You don't need to free data from a singleton -- it lasts until the program quits, so the dealloc method will never be invoked.
If you need to close a network connection, or something else that actually needs to be ended, you should do this in a "close" method and invoke the "close" method on the singleton in your applicationWillTerminate: method of your application delegate."
意思差不多和官方一样,应用退出时,dealloc方法不会被调用,并且他建议我避免在dealloc中去作网络或其他类似的必须终止的操作,而应该将这些操作放在applicationWillTerminate:中,以确定这些操作被执行.
所以关于这个问题的研究暂时就告一段落了,得出的结论是:
1.不用担心静态全局变量的内存的问题,系统会在应用程序结束之后,回收这些内存.
2.应用程序结束时会直接回收所有的程序运行中的资源,而不调用对象的dealloc方法.
3.不要将类似网络或文件的关闭(应该是任何)操作放在类的dealloc方法中执行.
https://blog.csdn.net/u010158696/article/details/51810695