AVFoundation: Cleanup AVCaptureSession with proper reference counting
authorAndy Nichols <andy.nichols@digia.com>
Tue, 4 Mar 2014 13:11:01 +0000 (14:11 +0100)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Tue, 4 Mar 2014 16:53:39 +0000 (17:53 +0100)
The reference counting was not being done for AVCaptureSession so it was
being destroyed before we got a chance to properly clean it up (which
lead to crashes).  We also make sure to remove any observers from
AVCaptureSession now before destroying it.

Task-number: QTBUG-37109
Task-number: QTBUG-29955
Change-Id: Ia9b49ad1eab01b4f7424e2a1c699d903cd9bf902
Reviewed-by: Yoann Lopes <yoann.lopes@digia.com>
src/plugins/avfoundation/camera/avfcamerasession.mm

index 042855a..a72ef50 100644 (file)
@@ -85,6 +85,7 @@ QMap<QByteArray, AVFCameraInfo> AVFCameraSession::m_cameraInfo;
     self->m_session = session;
     self->m_captureSession = session->captureSession();
 
+    [m_captureSession retain];
     [[NSNotificationCenter defaultCenter] addObserver:self
                                              selector:@selector(processRuntimeError:)
                                                  name:AVCaptureSessionRuntimeErrorNotification
@@ -103,6 +104,22 @@ QMap<QByteArray, AVFCameraInfo> AVFCameraSession::m_cameraInfo;
     return self;
 }
 
+- (void) dealloc
+{
+    [[NSNotificationCenter defaultCenter] removeObserver:self
+                                                    name:AVCaptureSessionRuntimeErrorNotification
+                                                  object:m_captureSession];
+
+    [[NSNotificationCenter defaultCenter] removeObserver:self
+                                                    name:AVCaptureSessionDidStartRunningNotification
+                                                  object:m_captureSession];
+
+    [[NSNotificationCenter defaultCenter] removeObserver:self
+                                                    name:AVCaptureSessionDidStopRunningNotification
+                                                  object:m_captureSession];
+    [m_captureSession release];
+    [super dealloc];
+}
 
 - (void) processRuntimeError:(NSNotification *)notification
 {