我之前发过几个问题,试图解决为什么我得到一个EXC_BAD_ACCESS,我做了一些调试
见这里: Help debugging iPhone app - 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
所以,我的猜测是我正在使用的视图在获取由imagepicker引起的内存警告后被卸载...
我的问题是,如何阻止这种情况发生?我显然需要调用PictureView的clearPage方法,但如果它被卸载我就不能。
答案 0 :(得分:3)
PictureView中是否保留了PictureView(我假设它是控制器视图的子视图)?如果它没有保留,那么你有一个悬空参考。当您的视图控制器不在最前面,并且它收到-didReceiveMemoryWarning消息时,默认情况下它将释放其视图成员。如果你有指向它的子视图的指针,并且它们没有保留,你将最终处于这种情况。
首先要尝试重写-didReceiveMemoryWarning并调用clearPage:
- (void) didReceiveMemoryWarning {
[myPictureView clearPage];
myPictureView = nil;
[super didReceiveMemoryWarning];
}