From df785e66089c7089a0ef82cdea1e6fa68b48b7d4 Mon Sep 17 00:00:00 2001 From: sudden6 Date: Thu, 19 Mar 2015 12:37:52 +0100 Subject: [PATCH] Add some error handling in attempt to fix #1240 This should prevent the application from crashing, but there is still no way to tell there is no webcam attached. --- src/video/cameraworker.cpp | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/video/cameraworker.cpp b/src/video/cameraworker.cpp index d4c40731d..47acb377a 100644 --- a/src/video/cameraworker.cpp +++ b/src/video/cameraworker.cpp @@ -146,7 +146,21 @@ void CameraWorker::subscribe() if (!cam.isOpened()) { queue.clear(); - cam.open(camIndex); + bool bSuccess = false; + + try + { + bSuccess = cam.open(camIndex); + } + catch( cv::Exception& e ) + { + qDebug() << "CameraWorker:" << "OpenCV exception caught: " << e.what(); + } + + if(!bSuccess) + { + qDebug() << "CameraWorker: Could not open camera"; + } applyProps(); // restore props } } @@ -166,7 +180,20 @@ void CameraWorker::doWork() if (!cam.isOpened()) return; - if (!cam.read(frame)) + bool bSuccess = false; + + try + { + bSuccess = cam.read(frame); + } + catch( cv::Exception& e ) + { + qDebug() << "CameraWorker:" << "OpenCV exception caught: " << e.what();; + this->clock->stop(); // prevent log spamming + qDebug() << "CameraWorker: stopped clock"; + } + + if (!bSuccess) { qDebug() << "CameraWorker: Cannot read frame"; return;