MAC QT5.13 delete QCamera class make corruption
-
I have a USB camera that not needed driver and In my writen code when the USB camera disappear from my computer, I will delete the QCamera class, but when I delete the QCamera, the program cooruption, here is the debug information,the last page is the corruption occur
Please helpt me, thanks
-
-
@mingzhi Maybe you should first call https://doc.qt.io/qt-5/qcamera.html#stop ?
-
@ChrisW67 In the Init ,I first use QCameraInfo class to finde all avaliable cameraInfo, and create a Qcamera class which I want camerInfo and add this cameraInfo to a QList, then create a Timertask to detect QCameraInfo and when the QCameraInfo::avaliables() doesn't have the QList's camerainfo, I send a signal to slot, in slot I delete QCamera
-
@ChrisW67 befort delete QCamera , unload, unlock, stop, all those I all try, here is the main create and dropp code, cameraConnected function and cameraDropped function are all slot fuction for cameraconnect signal and cameradrop signal
void MainWindow::cameraConnected( QList<QCameraInfo> connectedCameras )
{
foreach( const QCameraInfo& cameraInfo, connectedCameras ) {
if( false == fAvailableCameras.contains( cameraInfo ) ) {
fAvailableCameras.append( cameraInfo );if( nullptr == fCurrentCamera ) { setCamera( cameraInfo ); } } }
}
void MainWindow::setCamera( const QCameraInfo& cameraInfo )
{
delete fMediaRecorder;
delete fImageCapture;
delete fCurrentCamera;
delete fAudioProbe;fMediaRecorder = nullptr; fImageCapture = nullptr; fCurrentCamera = nullptr; fAudioProbe = nullptr; fCurrentCamera = new QCamera( cameraInfo ); connect( fCurrentCamera, SIGNAL( stateChanged( QCamera::State ) ), this, SLOT( updateCameraState( QCamera::State ) ) ); connect( fCurrentCamera, SIGNAL( error( QCamera::Error ) ), this, SLOT( cameraError( QCamera::Error ) ) ); fMediaRecorder = new QMediaRecorder( fCurrentCamera ); connect( fMediaRecorder, SIGNAL( stateChanged( QMediaRecorder::State ) ), this, SLOT( updateRecorderState( QMediaRecorder::State ) ) ); qDebug() << fMediaRecorder->videoSettings().encodingOptions(); qDebug() << fMediaRecorder->supportedContainers(); fImageCapture = new QCameraImageCapture( fCurrentCamera ); connect( fMediaRecorder, SIGNAL( durationChanged( qint64 ) ), this, SLOT( updateRecordTime( qint64 ) ) ); connect( fMediaRecorder, SIGNAL( error( QMediaRecorder::Error ) ), this, SLOT( recorderError( QMediaRecorder::Error ) ) ); fMediaRecorder->setMetaData( QMediaMetaData::Title, QVariant( QLatin1String( "SurgiCam" ) ) ); //connect( ui->exposureCompensation, SIGNAL(valueChanged(int)), SLOT(setExposureCompensation(int))) ; fCurrentCamera->setViewfinder( fCameraViewfinder->videoSurface() ); fCurrentCamera->imageProcessing()->setWhiteBalanceMode( QCameraImageProcessing::WhiteBalanceManual ); fCurrentCamera->imageProcessing()->setManualWhiteBalance( 50 ); fCurrentCamera->imageProcessing()->setBrightness( 0 ); fCurrentCamera->imageProcessing()->setContrast( 0 ); fCurrentCamera->imageProcessing()->setSaturation( 0 ); fCurrentCamera->imageProcessing()->setSharpeningLevel( 0 ); fCurrentCamera->imageProcessing()->setDenoisingLevel( 0 ); fAudioProbe = new QAudioProbe(); if( false == fAudioProbe->setSource( fCurrentCamera ) ) { connect( fAudioProbe, SIGNAL( audioBufferProbed( QAudioBuffer ) ), this, SLOT( updateAudioLevel( QAudioBuffer ) ) ); } fIsFullScreen = false; //connect( fActionToggleFullScreen, SIGNAL( toggled( bool ) ), fCameraViewfinder, SLOT( setFullScreen( bool ) ) ); connect( fActionToggleFullScreen, SIGNAL( toggled( bool ) ), this, SLOT( toggleFullScreenMode( bool ) ) ); connect( fActionToggleFullScreenNative, SIGNAL( toggled( bool ) ), this, SLOT( toggleFullScreenMode( bool ) ) ); updateCameraState( fCurrentCamera->state() ); updateCameraLockState( fCurrentCamera->lockStatus(), QCamera::UserRequest ); updateRecorderState( fMediaRecorder->state() ); connect( fImageCapture, SIGNAL( readyForCaptureChanged( bool ) ), this, SLOT( readyForCapture( bool ) ) ); connect( fImageCapture, SIGNAL( imageCaptured( int, QImage ) ), this, SLOT( capturedImage( int, QImage ) ) ); connect( fImageCapture, SIGNAL( imageSaved( int, QString ) ), this, SLOT( imageSaved( int, QString ) ) ); connect( fImageCapture, SIGNAL( error( int, QCameraImageCapture::Error, QString ) ), this, SLOT( captureError( int, QCameraImageCapture::Error, QString ) ) ); connect( fCurrentCamera, SIGNAL( lockStatusChanged( QCamera::LockStatus, QCamera::LockChangeReason ) ), this, SLOT( updateCameraLockState( QCamera::LockStatus, QCamera::LockChangeReason ) ) );
// ui->captureWidget->setTabEnabled(0, (camera->isCaptureModeSupported(QCamera::CaptureStillImage)));
// ui->captureWidget->setTabEnabled(1, (camera->isCaptureModeSupported(QCamera::CaptureVideo)));// updateCaptureMode();
fCurrentCamera->load(); qDebug() << "Supported audio codec: " << fMediaRecorder->supportedAudioCodecs(); qDebug() << "Supported audio sample-rate: " << fMediaRecorder->supportedAudioSampleRates(); qDebug() << "Supported video codec: " << fMediaRecorder->supportedVideoCodecs(); qDebug() << "Supported video resolutions: " << fImageCapture->supportedResolutions(); qDebug() << "Supported video frame-rate: " << fMediaRecorder->supportedFrameRates(); fCurrentCamera->setCaptureMode( QCamera::CaptureStillImage ); fCurrentCamera->start(); fCurrentCameraInfo = cameraInfo; fMediaPlayerWidget->stop(); fStackedPlayersWidget->setCurrentIndex( 1 ); fWaitingForCameraWidget->stop(); fVideoEncoderSettings = fMediaRecorder->videoSettings(); fAudioEncoderSettings = fMediaRecorder->audioSettings(); fVideoContainerFormat = QString(); fSupportedResolution.clear(); QList<QSize> supportedResolution = fImageCapture->supportedResolutions(); for( int i = supportedResolution.count() - 1; i >= 0; --i ) { fSupportedResolution.append( supportedResolution[i] ); if( 4 == fSupportedResolution.count() ) { break; } } if( fCurrentCameraInfo.description().startsWith( fSupportedCameraDescriptions[0] ) ) { fSelectedResolution = 0; } else { fSelectedResolution = 3; } for( int i = 0; i < 4; ++i ) { fActionResolutions[i]->setText( QString( "%1 x %2" ).arg( fSupportedResolution[i].width() ).arg( fSupportedResolution[i].height() ) ); fActionResolutions[i]->setChecked( i == fSelectedResolution ); } fSelectedRotation = 0; for( int i = 0; i < 4; ++i ) { fActionRotations[i]->setChecked( i == fSelectedRotation ); } fVideoEncoderSettings.setResolution( fSupportedResolution[fSelectedResolution] ); fMediaRecorder->setEncodingSettings( fAudioEncoderSettings, fVideoEncoderSettings ); fLiveMode = true; fInformationFileNameEdit->setText( fLiveFileName ); fInformationDoctorNameEdit->setText( fLiveDoctorName ); fInformationPatientNameEdit->setText( fLivePatientName ); fInformationHospitalNameEdit->setText( fLiveHospitalName ); fInformationCommentEdit->setText( fLiveComment ); setupIdleUIState();
}
void MainWindow::cameraDropped( QList<QCameraInfo> droppedCameras )
{
bool dropCurrent = false;foreach( const QCameraInfo& cameraInfo, droppedCameras ) { if( false != fAvailableCameras.removeOne( cameraInfo ) ) { if( nullptr != fCurrentCamera && cameraInfo == fCurrentCameraInfo ) { if( nullptr != fMediaRecorder ) { if( QMediaRecorder::RecordingState == fMediaRecorder->state() ) { fMediaRecorder->stop(); } if( nullptr != fFileSplitTask ) { fFileSplitTask->abort(); fFileSplitTask = nullptr; } } fVideoCaptureAnimationLabel->toggle( false ); fVideoCaptureButton->setChecked( false ); delete fMediaRecorder; delete fImageCapture; delete fCurrentCamera; delete fAudioProbe; fMediaRecorder = nullptr; fImageCapture = nullptr; fCurrentCamera = nullptr; fAudioProbe = nullptr; fCurrentCameraInfo = QCameraInfo(); fMediaPlayerWidget->stop(); fStackedPlayersWidget->setCurrentIndex( 0 ); fWaitingForCameraWidget->start(); setupInvaildUIState(); dropCurrent = true; } } } if( false != dropCurrent ) { if( 0 < fAvailableCameras.count() ) { setCamera( fAvailableCameras[0] ); } }
}
-
@mingzhi said in MAC QT5.13 delete QCamera class make corruption:
In the Init ,I first use QCameraInfo class to finde all avaliable cameraInfo, and create a Qcamera class which I want camerInfo and add this cameraInfo to a QList, then create a Timertask to detect QCameraInfo and when the QCameraInfo::avaliables() doesn't have the QList's camerainfo, I send a signal to slot, in slot I delete QCamera
So, you will not detect the absence of the camera until some indeterminate time after the device has actually gone. I do not know whether your camera object pointer will still be valid at that point in time.
Have you looked at the QCamera::statusChanged() signal to see what that Qt is telling you before the crash?
-
@ChrisW67 if before delete QCamera I use stop and unload as the follow:
(fCurrentCamera is a QCamera class)
fCurrentCamera->stop(); ---------( QCamera::statusChanged() ->LoadedState)
fCurrentCamera->unload(); ---------( QCamera::statusChanged() ->UnloadedState)
delete fCurrentCamera; ---------(crash)and if I not used stop and unload to delete QCamera directly The QCamera::statusChanged() will not happened if I out USB Camera
-
@ChrisW67 sorry I also test now, if I not used stop and unload to delete QCamera directly The QCamera::statusChanged() will be UnloadedState after I use delete QCamera and then crash, but the Crash occurred during the delete process and before I delete QCamera I have detected the Camera removed from my computer