Question

I am writing an iOS6 app using Cocos2D with ARC turned on (Cocos is linked as a static library, not under ARC). I am able to present the camera using the following code:

cameraController = [[UIImagePickerController alloc] init];
// set other properties of camera
cameraController.delegate = psImageLayer;
psImageLayer.imagePicker = cameraController;
[[CCDirector sharedDirector] presentViewController:cameraController animated:YES completion:nil];

and I dismiss the camera in psImageLayer with this code:

- (void) imagePickerController: (UIImagePickerController *) picker
didFinishPickingMediaWithInfo: (NSDictionary *) info {
    // do something with image
    [[CCDirector sharedDirector] dismissViewControllerAnimated:YES completion:nil];
}

When I dismiss the camera, the app crashes with the following error: *** -[PLImageScrollView release]: message sent to deallocated instance 0x2494f4f0 I am pretty sure that PLImageScrollView is an iOS class, because I did not write it.

My issue appears to be very similar to the issue posted here, but his solution involves modifying the class that owns the delegate. In this case, UIImagePickerController is the class, which cannot be modified.

The relevant parts of the PhotoShareImageLayer header file are posted below:

// PhotoShareImageLayer.h (this is what psImageLayer is)
@interface PhotoShareImageLayer : CCLayer <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property(nonatomic, retain) UIImagePickerController *imagePicker;
@property(nonatomic, retain) UIImage *currentImage;

@end

Any ideas on how to stop this error from happening? Thanks.

EDIT: List of things I have already tried.

  • Subclassing UIImagePickerController and adding - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self setDelegate:nil]; }

EDIT 2: The crash does not happen in imagePickerController:didCancel. Only when a picture is taken, OR when "Retake" is pressed in the camera. There is no UIImagePickerDelegate method for "Retake" (only "Cancel" and "Use").

EDIT 3: After continuing and writing more of the app, it appears this issue is not unique to the camera. The same (or very similar) errors occur when dismissing modal views for Twitter, Facebook, Contacts, and more.

Was it helpful?

Solution

I believe this is a problem with Apple's internal implementation of PLImageScrollView. I'm swizzling the UIScrollView`s setDelegate method and this causes a crash when a UIImagePicker is used (although, it only seems to be if you select a photo, not if you cancel). The problem I end up seeing is that the scrollViewDidScroll: method is sent to the real delegate (through my interceptor), but it's already been released.

This suggests to me that the PLImageScrollView's delegate is being dealloced without being nilled out. I half-solved the problem by creating my own strong reference to the real delegate. This could cause a memory leak in other implementations, but that's better than a crash at least.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top