R – View being released when in use due to memory warnings


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


Overreleasing here?

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.

Best Solution

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:

- (void) didReceiveMemoryWarning {
    [myPictureView clearPage];
    myPictureView = nil;
    [super didReceiveMemoryWarning];
Related Question