【问题标题】:How to clean up AVCaptureSession in applicationDidEnterBackground?如何清理 applicationDidEnterBackground 中的 AVCaptureSession?
【发布时间】:2011-06-24 23:11:12
【问题描述】:

我有一个使用 AVCaptureSession 处理视频的应用。我喜欢写零内存泄漏,并正确处理所有对象。

这就是为什么这篇文章 - How to properly release an AVCaptureSession - 非常有帮助 - 因为 [session stopRunning] 是异步的,所以您不能只是停止会话并继续释放持有的对象。

这样就解决了。这是代码:

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

现在我来支持应用程序中断作为电话或按主页按钮。如果应用程序进入后台,我想停止捕获,并弹出我的视图控制器。

我似乎无法在 applicationDidEnterBackground 上下文中执行此操作。永远不会调用 dealloc,我的对象仍然存在,当我重新打开应用程序时,框架会开始自动进入。

我尝试使用 beginBackgroundTaskWithExpirationHandler 但无济于事。变化不大。

有什么建议吗? 谢谢!

【问题讨论】:

    标签: ios4 background camera release avcapturesession


    【解决方案1】:

    我没有你的问题的答案。 但我也阅读了thread you mentioned,我正在尝试实现它。 我很惊讶你在 stopCapture 函数中有这段代码:

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

    我认为代码是会话初始化的一部分。这对你有用吗?

    您的 capture_cleanup 函数是否被调用?我的没有被调用,我正在尝试找出原因。

    【讨论】:

    • 嗨,这个代码确实在我的清理代码中找到。可能是复制粘贴错误,但它有效。 capture_cleanup 确实被调用了。我想我替换了 dataOutput 队列,然后关闭它,所以适当地调用了清理。但你也可以问最先发布此代码的人 :) GL!
    • 在我的情况下同样的问题, capture_cleanup 没有被调用。有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多