Search code examples
iosgpuimage

GPUImage simple code crash on execution


This code crashes about 1 second after it is excuted (iOS7) :

-(void)initializeCamera
{
    GPUImageStillCamera *stillCamera=[[GPUImageStillCamera alloc]initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];
    stillCamera.outputImageOrientation=UIInterfaceOrientationPortrait;
    GPUImageView *image=[[GPUImageView alloc]initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];
    GPUImageGrayscaleFilter *filter=[[GPUImageGrayscaleFilter alloc]init];

    [self.view addSubview:image];
    [stillCamera addTarget:filter];
    [filter addTarget:image];
    [stillCamera startCameraCapture];
}

If it's any help just before crash a picture of what the camera captured is displayed.

GPUImage is installed with CocoaPod :

platform :ios, '7.0'
pod 'GPUImage'

Solution

  • That's because you're not retaining your GPUImageStillCamera. You're creating it as a local instance within the -initializeCamera method and not holding on to it, so it will be deallocated the instant that -initializeCamera completes, causing a crash or other undefined behavior.

    You need to hold on to this as a property or instance variable on the class that -initializeCamera resides within, or some other external strong reference.