简体   繁体   中英

iOS OpenGL/GPUImage crash when entering background to foreground

We are getting a crash that happens with only certain types of applicationDidBecomeActive and DidEnterBackground with iOS OpenGL/GPUImage preview screen. Specifically, it happens only if the power button is pressed/released to background the app. It doesn't happen if the home button is clicked and app sent to background and reactivated. We are not calling into any OpenGL in the background [have read the Apple DO's and DONT's] - app doesn't do anything in the BG and have tried stopping in DidResign itself.

The GPU calls are made only if there is a new video capture frame from the device sensor, which I presume will be suspended by iOS when it enters background. So the below stack trace could only be firing when the app is re-started or just before it officially suspended ???

Does anyone know of any Apple or other iOS/OpenGL alloc/release protocol one needs to follow when app enters bg and reactivated? More importantly, is there a clean way to clear/release all the OpenGL/GPUImage framebuffers/textures/context, etc and reinitialize everything?


Part of the stack trace is as below where it crashes with EXC_BAD_ACCESS:

#6  0x003635fe in -[GPUImageContext presentBufferForDisplay] at 
#7  0x0035c416 in -[GPUImageView presentFramebuffer] at 
#8  0x0035cb4a in __44-[GPUImageView newFrameReadyAtTime:atIndex:]_block_invoke at 
#9  0x00363d9e in runSynchronouslyOnVideoProcessingQueue at 
#10 0x0035c89e in -[GPUImageView newFrameReadyAtTime:atIndex:] at 
#11 0x00357a12 in -[GPUImageFilter informTargetsAboutNewFrameAtTime:] at 

Not sure if I had the same problem but I did get a EXC_BAD_ACCESS error when application was put in background.

I was able to solve it by pausing (and resuming) the video preview when app enters background.

Bind the application to notify you of background/foreground activity after GPUImageVideoCamera startCameraCapture :

- (void)addObservers {
    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(applicationDidEnterBackground:)
                                                name:UIApplicationDidEnterBackgroundNotification
                                              object:nil];

    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(applicationDidBecomeActive:)
                                                name:UIApplicationDidBecomeActiveNotification
                                              object:nil];
}

And add these after stopping the camera with GPUImageVideoCamera stopCameraCapture and in your dealloc method:

- (void)removeObservers {
    [[NSNotificationCenter defaultCenter]removeObserver:self
                                                   name:UIApplicationDidEnterBackgroundNotification
                                                 object:nil];

    [[NSNotificationCenter defaultCenter]removeObserver:self
                                                   name:UIApplicationDidBecomeActiveNotification
                                                 object:nil];
}

And use these callbacks for the state changes (I'm unsure if there is any namespace collision with these method names, although I haven't had any):

- (void)applicationDidBecomeActive:(NSNotification *)notification {
    [self.videoCamera resumeCameraCapture];
}

- (void)applicationDidEnterBackground:(NSNotification *)notification {
    // TODO: Stop recording if recording in progress
    [self.videoCamera pauseCameraCapture];
}

I had a similar issue with a photo processing app that uses open gl shaders to do it's thing.

It could be that when going to the background and coming back the OpenGL shader program is no longer there.

On returning assume that the shader program needs to be recompiled. Looking in the gpuimage samples I'm not sure there is a simple way to tell each of the shaders that the program isn't compiled any longer.

You might want to report it as a bug on Brad Larson's github . Or you could try on returning to foreground destroying and rebuilding the all the targets/filters.

I also now see that this question is several months old and you've probably found a solution by now. It would be great if you could post an answer to your own question to help out the SO community.

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