由于内存警告,视图在使用时被释放
我之前已经发布了几个问题,试图找出为什么我得到 EXC_BAD_ACCESS,并且我已经做了一些调试,
请参见这里: 帮助调试 iPhone 应用 - EXC_BAD_ACCESS
和:
所以,我认为我已经发现发生了什么。
从我的日志中,我得到了这样的信息:
Exception Type: EXC_BAD_ACCESS (SIGBUS)
Exception Codes: KERN_PROTECTION_FAILURE at 0x0000000f
Crashed Thread: 0
Thread 0 Crashed:
0 libobjc.A.dylib 0x00003ebc objc_msgSend + 20
1 MyApp 0x0000378a -[PictureView clearPage] (PictureView.m:79)
起初我认为问题是在“clearPage”函数中发送消息的已释放对象,但是自从与朋友交谈后,我相信 PictureView 对象本身可能已经被释放。
我的日志输出证实了这一点:
Fri Jan 8 20:28:32 unknown MyApp[2224] <Warning>: Picture View Unloaded
Fri Jan 8 20:28:32 unknown MyApp[2224] <Warning>: Memory warning from Picture view
Fri Jan 8 20:28:34 unknown MyApp[2224] <Warning>: Scaling image
Fri Jan 8 20:28:36 unknown MyApp[2224] <Warning>: Attempting to save image to disk
Fri Jan 8 20:28:37 unknown MyApp[2224] <Warning>: Saved file to: /var/mobile/Applications/065C0D37-95C1-41D4-98F0-16A3555682CD/Documents/MyImage1.jpg
Fri Jan 8 20:28:37 unknown MyApp[2224] <Warning>: Clearing page
Fri Jan 8 20:28:40 unknown ReportCrash[2225] <Notice>: Formulating crash report for process MyApp[2224]
Fri Jan 8 20:28:41 unknown com.apple.launchd[1] <Warning>: (UIKitApplication:com.yourcompany.MyApp[0xb8e1]) Job appears to have crashed: Bus error
所以,我的猜测是,在收到图像选择器引起的内存警告后,我正在使用的视图正在被卸载......
我的问题是,如何阻止这种情况发生?我显然需要调用PictureView的clearPage方法,但如果它被卸载,我就不能调用。
I've posted a couple of questions before, trying to work out why I'm getting a EXC_BAD_ACCESS, and I've done a bit of debugging
See here:
Help debugging iPhone app - EXC_BAD_ACCESS
and:
So, I think I've discovered what's going on.
From my log, I'm getting this:
Exception Type: EXC_BAD_ACCESS (SIGBUS)
Exception Codes: KERN_PROTECTION_FAILURE at 0x0000000f
Crashed Thread: 0
Thread 0 Crashed:
0 libobjc.A.dylib 0x00003ebc objc_msgSend + 20
1 MyApp 0x0000378a -[PictureView clearPage] (PictureView.m:79)
At first I thought the problem was a released object being sent a message within the 'clearPage' function, however since talking to a friend, I am led to believe that the PictureView object itself might already be released.
This is confirmed by my logging output:
Fri Jan 8 20:28:32 unknown MyApp[2224] <Warning>: Picture View Unloaded
Fri Jan 8 20:28:32 unknown MyApp[2224] <Warning>: Memory warning from Picture view
Fri Jan 8 20:28:34 unknown MyApp[2224] <Warning>: Scaling image
Fri Jan 8 20:28:36 unknown MyApp[2224] <Warning>: Attempting to save image to disk
Fri Jan 8 20:28:37 unknown MyApp[2224] <Warning>: Saved file to: /var/mobile/Applications/065C0D37-95C1-41D4-98F0-16A3555682CD/Documents/MyImage1.jpg
Fri Jan 8 20:28:37 unknown MyApp[2224] <Warning>: Clearing page
Fri Jan 8 20:28:40 unknown ReportCrash[2225] <Notice>: Formulating crash report for process MyApp[2224]
Fri Jan 8 20:28:41 unknown com.apple.launchd[1] <Warning>: (UIKitApplication:com.yourcompany.MyApp[0xb8e1]) Job appears to have crashed: Bus error
So, my guess is the view that I'm using is being unloaded after getting a memory warning caused by the imagepicker...
And my question is, how can I stop this from happening? I obviously need to call the clearPage method of the PictureView, but I can't if it's unloaded.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
PictureView 是否保留在您的控制器中(我假设它是控制器视图的子视图)?如果没有保留,那么您就有了一个悬空引用。当您的视图控制器不是最前面的,并且它收到 -didReceiveMemoryWarning 消息时,默认情况下它将释放其视图成员。如果您有指向其子视图的指针,并且它们没有被保留,那么您最终会遇到这种情况。
首先要尝试的是覆盖 -didReceiveMemoryWarning 并调用clearPage:
Is PictureView retained in your controller (I'm assuming it's a subview of your controller's view)? If it's not retained, then you've got a dangling reference. When your view controller is not frontmost, and it receives a -didReceiveMemoryWarning message, by default it will release its view member. If you have pointers into subviews of that, and they're not retained, you'll end up in this situation.
The first thing to try is overriding -didReceiveMemoryWarning and calling clearPage: