Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to clean up AVCaptureSession in applicationDidEnterBackground?
    text
    copied!<p>I have an app that uses AVCaptureSession to process video. I like to write with zero memory leaks, and proper handling of all objects.</p> <p>That's why this post - <a href="https://stackoverflow.com/questions/3741121/how-to-properly-release-an-avcapturesession">How to properly release an AVCaptureSession</a> - was tremendously helpful - Since <strong>[session stopRunning]</strong> is asynchronous, you can't just stop the session and continue to release the holding object. </p> <p>So that's solved. This is the code:</p> <pre><code>// Releases the object - used for late session cleanup static void capture_cleanup(void* p) { CaptureScreenController* csc = (CaptureScreenController*)p; [csc release]; // releases capture session if dealloc is called } // Stops the capture - this stops the capture, and upon stopping completion releases self. - (void)stopCapture { // Retain self, it will be released in capture_cleanup. This is to ensure cleanup is done properly, // without the object being released in the middle of it. [self retain]; // Stop the session [session stopRunning]; // Add cleanup code when dispatch queue end dispatch_queue_t queue = dispatch_queue_create("capture_screen", NULL); dispatch_set_context(queue, self); dispatch_set_finalizer_f(queue, capture_cleanup); [dataOutput setSampleBufferDelegate: self queue: queue]; dispatch_release(queue); } </code></pre> <p>Now I come to support app interruptions as a phone call, or pressing the home button. In case application enters background, I'd like to stop capturing, and pop my view controller.</p> <p>I can't seem to do it at the applicationDidEnterBackground context. dealloc is never called, my object remains alive, and when I reopen the app the frames just start coming in automatically.</p> <p>I tried using beginBackgroundTaskWithExpirationHandler but to no avail. It didn't change much.</p> <p>Any suggestions? Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload