How to clear AVCaptureSession in applicationDidEnterBackground?

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!

+4
source share
1 answer

I have no answer to your question. But I also read the thread you mentioned , and I'm trying to implement it. I am surprised that you have this code in the stopCapture function:

 // 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); 

I thought the code was needed as part of the session initialization. Does this work for you?

Is the call to the capture_cleanup function called? mine is not called, and I'm trying to understand why.

0
source

Source: https://habr.com/ru/post/1339231/


All Articles