The program has unexpectedly finished
-
Hi,
I'm trying to have my webcam capture still images. I'm building the code step by step. So far, I have the code to check if a camera is available and display an error if no cams available. The code complies and run but as soon as I click the open cam button on mainWindow, the program stops working. Here is the code for the camera dialog:
camera.h
#ifndef CAMERA_H #define CAMERA_H #include <QDialog> #include <QCamera> #include <QCameraImageCapture> namespace Ui { class Camera; } class Camera : public QDialog { Q_OBJECT public: explicit Camera(QWidget *parent = 0); ~Camera(); private slots: void on_cancelBtn_clicked(); private: Ui::Camera *ui; QCamera *camera; QCameraImageCapture *imageCapture; }; #endif // CAMERA_HCamera.cpp
#include "camera.h" #include "ui_camera.h" #include <QCameraInfo> #include <QMessageBox> Camera::Camera(QWidget *parent) : QDialog(parent), ui(new Ui::Camera) { ui->setupUi(this); //Camera code begins here if (QCameraInfo::availableCameras().count() > 0) { QList<QCameraInfo> cameras = QCameraInfo::availableCameras(); foreach (const QCameraInfo &cameraInfo, cameras) { if (cameraInfo == QCameraInfo::defaultCamera()) camera = new QCamera(cameraInfo); } } else { QMessageBox::warning(this, tr("No Camera is available Error"), camera->errorString()); } } Camera::~Camera() { delete ui; delete imageCapture; delete camera; } void Camera::on_cancelBtn_clicked() { close(); }What am I doing wrong?
-
If "open cam" crashes the app it would be nice to see the code of it. Otherwise it's guessing.
Other than that - you're not zero-initializing your pointer variables
cameraandimageCapture. They have a garbage value.
If there are no cameras thecamerapointer is uninitialized and so callingcamera->errorString()is a bug. -
If "open cam" crashes the app it would be nice to see the code of it. Otherwise it's guessing.
Other than that - you're not zero-initializing your pointer variables
cameraandimageCapture. They have a garbage value.
If there are no cameras thecamerapointer is uninitialized and so callingcamera->errorString()is a bug.Here is the code inside "open cam"
#include "mainwindow.h" #include "ui_mainwindow.h" #include "camera.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { Camera cam; cam.setModal(true); cam.setWindowFlags(Qt::CustomizeWindowHint); //Set window with no title bar cam.setWindowFlags(Qt::FramelessWindowHint); //Set a frameless window cam.exec(); }I disconnected the cam to find out if it will be able to detect that there are no cameras. How can I get around the camera pointer is uninitialized?
-
How can I get around the camera pointer is uninitialized?
Well, by initializing it ;)
Camera::Camera(QWidget *parent) : QDialog(parent), ui(new Ui::Camera), camera(nullptr), imageCapture(nullptr) { ...and not calling it when it's not initialized:
{ QMessageBox::warning(this, tr("Error"), tr("No Camera is available.")); } -
Here is the code inside "open cam"
#include "mainwindow.h" #include "ui_mainwindow.h" #include "camera.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { Camera cam; cam.setModal(true); cam.setWindowFlags(Qt::CustomizeWindowHint); //Set window with no title bar cam.setWindowFlags(Qt::FramelessWindowHint); //Set a frameless window cam.exec(); }I disconnected the cam to find out if it will be able to detect that there are no cameras. How can I get around the camera pointer is uninitialized?
-
@Wilk The Camera object is a dialog and a blocking
exec()is called on it. It only goes out of scope when it's already closed and that is the preferred way to use dialogs. The crash is most likely due to the usage of uninitialized variables.