I have an application that uses AVCaptureSession to process video. I like to write with zero memory leaks and the proper handling of all objects.
This is why this post - How to Free AVCaptureSession Properly - was extremely useful - because [session stopRunning] is asynchronous, you can 'just end the session and continue to free the holder object.
So it decided. This is the 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); }
Now I come to support application interrupts as a phone call or by pressing the home button. In case the application goes into the background, I would like to stop capturing and place my controller.
I cannot do this in the context of applicationDidEnterBackground. dealloc is never called, my object remains alive, and when I open the application again, the frames will automatically turn on.
I tried using beginBackgroundTaskWithExpirationHandler, but to no avail. This has not changed much.
Any suggestions? Thanks!
source share