PDA

View Full Version : QMediaPlayer crash



FlyDoodle
5th October 2019, 20:44
Hello !
so i have this function in a class that plays an audio file and stops it if it's being active.

This is the class and its function :

#include "soundeffects.h"
#include <QMediaPlayer>
#include <QMediaPlaylist>

// klas sound effectov sm naredu in potem jih sam klicm ko jih rabm na primer ko pritisnem SPACE se zazene
// canonBallShot funkcija in sound effect ki se nahaj v tej funkciji;

SoundEffects::SoundEffects(){
soundEffect = new QMediaPlayer;
}

void SoundEffects::playPlaneEngineSound(){

soundEffect->setMedia(QUrl("qrc:/Story_Sounds/PlaneEngine.wav"));

if(soundEffect->state() == QMediaPlayer::PlayingState){
soundEffect->stop();
}else if(soundEffect->state() == QMediaPlayer::StoppedState){
soundEffect->play();
}
}


I call the function in game.cpp 3 times like this:

soundEffects->playPlaneEngineSound();
soundEffects->playPlaneEngineSound();
SoundEffects->playPlaneEngineSound();
and the application crashes and i don't know why...
Could it be because i set media again when the function is called the third time ?
And if how do i fix it so that when i call the function once, it plays the sound, second time it stops it and so on ( checking if it's active or not and depending on the output the audio is played or stopped ).

ChrisW67
6th October 2019, 08:25
and the application crashes and i don't know why...
Could it be because i set media again when the function is called the third time ?
And if how do i fix it so that when i call the function once, it plays the sound, second time it stops it and so on ( checking if it's active or not and depending on the output the audio is played or stopped ).
We can only guess given the information supplied. My guess is that your soundEffects pointer becomes invalid after the second use.

The error message and the stack backtrace would be useful.

FlyDoodle
6th October 2019, 12:37
The error message and the stack backtrace would be useful.
Here you go:
13270
And
13271

It's a segmentation fault which means that im trying to access something that doesn't exist ?
It crashes when the function is called the second time

ChrisW67
9th October 2019, 09:56
Ok. So what is m_device set to at the time of the SIGSEGV?

FlyDoodle
10th October 2019, 14:32
Sorry but i don't know how to check that...
I found this if it helps at all ?
13272

13272

ChrisW67
15th October 2019, 09:56
Given that the segfault is occurring at that line it is likely that m_device is either nullptr or, I suspect more likely, a value that was previously pointing at a valid object but is no longer.
In the debugger screenshot at post #3, in the top left, expand "this" and look for "m_device" to see its value. Zero? You can put a breakpoint here to see it on each pass.

The m_device member variable is set at construction of your DirectShowIOReader, and is valid for some period after that if it only fails on the second pass through readyRead(). The ownership and lifespan of the QIODevice object passed in needs to be considered... does it get deleted or go out of scope?

Another complication may be the use of threads here.