简体   繁体   中英

how to fix over released objects

NSZombie detected that one of the objected is over released in my app and that is causing the app to crash every time when a button is pressed. However, after inspecting the source code of where the over release happens, I couldn't see any obvious code that that may have caused the release. Can xcode release objects automatically without any actual code?

Below are the places reported by the Instrument that has a release event:

-(void) takePicture:(CDVInvokedUrlCommand *)command {
    CDVCameraPicker* cameraPicker = [[CDVCameraPicker alloc] init];
    self.pickerController = cameraPicker;
    CameraOverlayViewController* overlay = [[CameraOverlayViewController alloc] initWithNibName:@"CameraOverlayViewController" bundle:nil];
    cameraPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    cameraPicker.cameraOverlayView = overlay.view;

    [self.viewController presentViewController:cameraPicker animated:YES completion:nil];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

Any ideas on how to fix the problem? Thanks in advance!

I think CameraOverlayViewController is released.

Try the below code

CameraOverlayViewController* overlay;
    -(void) takePicture:(CDVInvokedUrlCommand *)command {
    CDVCameraPicker* cameraPicker = [[CDVCameraPicker alloc] init];
    self.pickerController = cameraPicker;
    overlay = [[CameraOverlayViewController alloc] initWithNibName:@"CameraOverlayViewController" bundle:nil];
    cameraPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    cameraPicker.cameraOverlayView = overlay.view;

    [self.viewController presentViewController:cameraPicker animated:YES completion:nil];
}

You're creating a CameraOverlayViewController and keeping a reference to it's view, but the CameraOverlayViewController itself is going out of scope when the takePicture: method ends. A view doesn't retain it's view controller. You need to either keep a reference to the CameraOverlayViewController, or if you're only interested in the view object itself, just create a view from the xib file and set the cameraPicker.cameraOverlayView to that view.

EDIT:

For example, in your header file you could make a property:

@property (strong) CameraOverlayViewController *overlay;

and in your method:

-(void) takePicture:(CDVInvokedUrlCommand *)command {
    CDVCameraPicker* cameraPicker = [[CDVCameraPicker alloc] init];
    self.pickerController = cameraPicker;

    self.overlay = [[CameraOverlayViewController alloc] initWithNibName:@"CameraOverlayViewController" bundle:nil];
    cameraPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    cameraPicker.cameraOverlayView = self.overlay.view;

    [self.viewController presentViewController:cameraPicker animated:YES completion:nil];
}

Once you're done with the camera, you'll want to let go of the reference. So for example:

-(void) cameraPickerFinished {
   self.overlay = nil;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM