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_H
    
    

    Camera.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?


  • Moderators

    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 camera and imageCapture. They have a garbage value.
    If there are no cameras the camera pointer is uninitialized and so calling camera->errorString() is a bug.



  • @Chris-Kawa

    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?


  • Moderators

    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."));
    }
    


  • @WhatIf Hi
    Try to create a pointer to Camera object instead of object itself in you slot. I guess that you Camera object may just be destroyed when it gets out of scope.


  • Moderators

    @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.