如何判断引用的对象是否已在 Objective-C 中释放?
升级到 iPhone OS 4.0 后,在应用程序之间切换后应用程序开始崩溃。当应用程序收到内存警告时,应用程序会在同一位置崩溃。
当收到内存警告时,某些对象似乎会自动释放,然后当我们尝试使用释放的对象时,应用程序崩溃了。
是否可以测试对象是否已被释放,以便我们可以重新分配它们?
After upgrading to iPhone OS 4.0 the application started to crash after switching between applications. The application crashes in the same place when the application receives a memory warning.
It seems like some objects are automatically deallocated when a memory warning is received and then when we try to use the deallocated objects the application crashes.
Is it possible to test if an object has been deallocated, so that we can reallocate them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法测试特定对象是否已被释放,因为释放后,该对象不再存在。您唯一能做的就是测试另一个对象对可疑对象的引用是否仍然为非零。
这里的问题不是释放本身,而是保留管理不善。您有一个对象已被标记为不再使用,并且系统正在按系统应有的方式杀死它。您在内存不足时看到它的唯一原因是系统立即停止并耗尽所有释放池,而不是等待正常周期。
您需要确保已正确保留所需的所有对象,以免它们被不当释放。即使在内存不足的情况下,保留的对象也不会被释放。
编辑
我想补充一点,低内存崩溃的最常见原因是假设视图或视图中的资源始终存在,即使视图未显示也是如此。系统将清除低内存中未显示的视图及其资源(如图像)。检查视图控制器的
didReceiveMemoryWarning
。You can't test whether a specific object has been deallocated because after deallocation, the object no longer exist. The only thing you can do is test whether a reference to the suspected object from another object is still non-nil.
Your problem here isn't deallocation per se but rather mismanaged retention. You have an object that has been marked as no longer being in use and the system is killing it as the system should. The only reason that you see it during low memory is that the system stops and drains all the release pool instantly instead of waiting for the normal cycle.
You need to make sure you have properly retained all the objects you need so they won't be improperly released. A retained object is not deallocated even in low-memory situations.
Edit
I would add that the most common cause of low-memory crashes is assuming that a view or a resource in a view is always present even when the view is not displayed. The system will purge undisplayed views and their resources (like images) in low-memory. Check the
didReceiveMemoryWarning
of the view controllers.您可以添加
如果正确的话,
并将其保留为空,然后在其中添加断点。此答案正确于
ARC
和NON-ARC
You can add
And leave it empty if it's the right to do, and add breakpoint in it.
This answer correct to
ARC
andNON-ARC
在
UIViewController
中实现dealloc方法以查看它从内存中释放的时刻- (void) dealloc
打印您要测试的任何对象的引用。
NSLog("获取指针:%@", self); // ViewController
然后在要测试对象是否仍然存在的地方设置断点。如果遇到断点,请使用
这里可以看到,dealloc方法打印日志后,指针不再可用,不再有对象了。
Implement the method dealloc inside a
UIViewController
to see the moment it is being released from memory- (void) dealloc
Print the reference of any object you want to test.
NSLog("Get pointer: %@", self); // ViewController
Then set a break point at a place where you want to test if the object is still existing. If you ran into the breakpoint, check the objects pointer in the debugger with
Here you can see, the pointer is not available anymore, there is no object anymore after the dealloc method printed a log.