Skip to content

Crash when app goes in background. #197

Open
@kinjaldua

Description

@kinjaldua

Hi,
I am using this project in my app to record videos and save them to the camera roll. However, I am facing an issue. When the app moves to the background, it crashes and gives "gpus_ReturnNotPermittedKillClient" error which I think comes due to OpenGL calls. I searched on the internet for a solution and found a solution here: http://www.cocos2d-iphone.org/forum/topic/30419
I am unable to understand how to implement that solution since GPU Image project doesn't have an app delegate class. Can you please help me out in this?

Thanks

Activity

BradLarson

BradLarson commented on Jun 17, 2012

@BradLarson
Owner

Any use of OpenGL ES when the application is in the background will cause the application to be killed. What are you trying to do in GPUImage when your application is going to the background and getting killed?

iamcam

iamcam commented on Aug 3, 2012

@iamcam
Contributor

I noticed this happens when the camera preview is active when the home or lock buttons are pressed.

ordinary

ordinary commented on Oct 9, 2012

@ordinary

Can you solve it ? I meet it too, can you help me?

cgdi

cgdi commented on Oct 11, 2012

@cgdi

I have this problem too. I tried to register for a notification to run "stopCameraCapture" when applicationWillResignActive gets called in the appDelegate, but the crash still occurs. Stopping the camera via a button action before moving the app into the background works though, however it requires the user to stop the camera himself.

update*
it doesn't crash anymore when I run "pauseCameraCapture" when applicationWillResignActive gets called. Similarly, run "resumeCameraCapture" when your app enters foreground to use the camera again.

joncampbell

joncampbell commented on Oct 23, 2012

@joncampbell
Contributor

I'm having the same crash issue. Pausing the capture when we applicationWillResignActive works if the user pressed the home button, but not the power button. It also doesn't crash if I place a breakpoint on applicationWillResignActive.

rizzow

rizzow commented on Oct 24, 2012

@rizzow

Calling [pauseCameraCapture] seems to resolve the issue for me.

K4stor

K4stor commented on Nov 6, 2012

@K4stor

Well the problem is that if I call stopCaptureStream I get an exception.
The problem is that the willResign method ends BEFORE the stream has stoped,
this also may cause the GL-thread to get a frame and process it...
thats why its all good when u use the debugger.

My fix:
U need to add an observer for AVCaptureSessionDidStopRunningNotification.
Instead of calling pause, we stop the capture
and wait in willResign until the onStop event has been fired.
then we call glfinish()!
Thats what the apple docu says. It wait until every GL thing is done so far
smth like this

- (void) onVideoStop
{
    DLog(@"video stoped");
    waitingForStreamToEnd = NO;
}

....

-(void) goToSleep
{
    waitingForStreamToEnd = YES;
    [stillCamera pauseCameraCapture];
    stillCamera = nil;  // I use ARC this calls release, and the deallc calls stopCameraCapture
    while(waitingForStreamToEnd)
    {

    }
     glFinish();
}

I hope this helps

iamcam

iamcam commented on Nov 7, 2012

@iamcam
Contributor

@j455 Interesting. That seems like a sound idea. I take it that this seems to fix the problem reliably, or are you still evaluating?

K4stor

K4stor commented on Nov 7, 2012

@K4stor

Tomorrow I will evaluate it depply...but atm I must say it fixes it for me...
Pls note that I kill the stillcam as well.
I only keep my texutres, that what the apple docu surgests

If it works... one can simply add a method into the Framework to wait for that thing to happend

From: Cameron Perry
Sent: Wednesday, November 07, 2012 5:51 PM
To: BradLarson/GPUImage
Cc: J455
Subject: Re: [GPUImage] Crash when app goes in background. (#197)

@j455 Interesting. That seems like a sound idea. I take it that this seems to fix the problem reliably, or are you still evaluating?


Reply to this email directly or view it on GitHub.

K4stor

K4stor commented on Nov 9, 2012

@K4stor

Nvm... doenst work neither

The only thing that really makes this "stable" is to add these two lines at the end of the will resign
sleep(2);
glFinish();

I think the problem is that

[videoOutput setSampleBufferDelegate:nil queue:dispatch_get_main_queue()];
[audioOutput setSampleBufferDelegate:nil queue:dispatch_get_main_queue()];

Use the main queue but it gets no time to complete during the resign method or smth...
fact is if I dont add the sleep
the destructor seems to be executed AFTER the will resign method was left.
And that (ofc) leads to an error...
As I just started with iOS I am not cool enough to fix that :(
but one of you nerdy hackers sure will be:D

bengotow

bengotow commented on Aug 1, 2013

@bengotow

Hi folks, just to summarize the findings here:

  • OpenGL may not be used when your app is in the background, so:
  • You should listen for the UIApplicationWillResignActiveNotification and UIApplicationWillEnterForegroundNotification notifications while your camera view is onscreen.
  • When your app is backgrounded, you need to stop GPUImage. Calling stopCameraCapture is asynchronous and doesn't stop using OpenGL immediately. This means that iOS will still kill your app sometimes, in spite of your best efforts to stop the camera in resignActive.
  • calling pauseCameraCapture is synchronous (it appears). Listening for resignActive and pausing camera capture reliably fixes the crash on the home button and power button press (for me at least.). The only trick is that you need to call resumeCameraCapture when your app returns to the foreground. Calling startCameraCapture when paused seems to have no effect.
K4stor

K4stor commented on Aug 1, 2013

@K4stor

Hi

Ye that sounds reasonable. one more thing is, when u pull down the status thing. The OS overall thing that shows the weather and latest feeds, Does your fix do the trick for that too ?

-Cheers,
Kastor

kolyuchiy

kolyuchiy commented on Nov 13, 2013

@kolyuchiy

Why "resign active" and not "did enter background"?

jessecurry

jessecurry commented on Apr 4, 2014

@jessecurry

didEnterBackground would be called after the app is in the background, willResignActive is called while the app is in the foreground, but will enter the background.

emmasteimann

emmasteimann commented on May 29, 2014

@emmasteimann

I know it's been a couple years since this is was first filed, but I'm still experiencing this exact problem.
I followed the advice and I'm running the following:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willResignActive) name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];`
...
- (void)willResignActive {
    [self.camera pauseCameraCapture];
}
- (void)didBecomeActive {
    [self.camera resumeCameraCapture];
}

Anyone have any thoughts?

henryl

henryl commented on Jun 26, 2014

@henryl

I am having the same issue as emmasteimann. This is happening for me on GPUImage 0.1.4 even with the proposed fix.

simpleshadow

simpleshadow commented on Jul 27, 2014

@simpleshadow

I recommend adding an observer for UIApplicationDidEnterBackgroundNotification as this allows the camera to still operate when double tapping the home button so the smaller app preview still shows the camera feed then pauses when actually put in the background by swapping to a different app.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willResignActive) name:UIApplicationDidEnterBackgroundNotification object:nil];
henryl

henryl commented on Jul 27, 2014

@henryl

I was able to solve this:

- (void)observeGlobalNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onApplicationWillResignActive) name:UIApplicationWillResignActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onApplicationDidBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
}

- (void)unobserveGlobalNotifications
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
}

- (void)onApplicationWillResignActive
{     
    [self.camera pauseCameraCapture];
    [self.camera stopCameraCapture];

    runSynchronouslyOnVideoProcessingQueue(^{
        glFinish();
    });
}

- (void)onApplicationDidBecomeActive
{
    [self.camera resumeCameraCapture];
    [self.camera startCameraCapture];
}
emmasteimann

emmasteimann commented on Aug 26, 2014

@emmasteimann

Thanks @henryl that for sure fixed it. Much appreciated! :-)

crossle

crossle commented on Apr 21, 2017

@crossle
MrHu4127

MrHu4127 commented on Jul 17, 2019

@MrHu4127

I do like this,but it also crash sometimes when app enterbackground in older iOS devices. self.movie is a instance of GPUImageMovie.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willResignActive) name:UIApplicationWillResignActiveNotification object:nil];

  • (void)willResignActive {
    [self.movie cancelProcessing];
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @iamcam@jessecurry@henryl@emmasteimann@joncampbell

        Issue actions

          Crash when app goes in background. · Issue #197 · BradLarson/GPUImage