Program crash when it loads videos from multiple directories continuously
-
I have code that loads videos from directories which are chosen by user.There will be many subdirs in listwidget , and the user can choose continuously.But when the user change the subdirectories quickly, the program crashed . Video player include play/pause button and postion slider(can skip,change position according to video playing time) . Here is the code .
connect (button , &QAbstractButton::clicked,[label,listWidget_main,listWidget_dirList,this]{ QDir directory = QFileDialog::getExistingDirectory(this, tr("Open Directory"),"/home", QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks); label->setText(directory.absolutePath()); QStringList dirs = directory.entryList(QDir::Dirs|QDir::NoDotAndDotDot); listWidget_dirList->addItems(dirs); connect(listWidget_dirList, & QListWidget::currentRowChanged,[listWidget_dirList,directory,listWidget_main,this](int currentRow) { //listWidget_dirList->setEnabled(false); listWidget_main->show(); listWidget_main->clear(); QListWidgetItem *item = listWidget_dirList->item(currentRow); QDir dir = directory.absolutePath() + '/'+item->text() ; dir.setNameFilters({"*.mp4" , "*.avi" , "*.flv" , "*.mwv"}); for(const QFileInfo & finfo: dir.entryInfoList()){ QMediaPlayer *mediaPlayer = new QMediaPlayer(); mediaPlayer->setMedia(QUrl::fromLocalFile(finfo.absoluteFilePath())); videoItem = new QGraphicsVideoItem; videoItem->setSize(QSize(320,240)); QGraphicsScene *scene = new QGraphicsScene(this); QGraphicsView *graphicsView = new QGraphicsView(scene); mediaPlayer->setVideoOutput(videoItem); QPushButton *m_playButton = new QPushButton(); m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay)); connect(m_playButton, &QAbstractButton::clicked, [mediaPlayer]() { switch (mediaPlayer->state()) { case QMediaPlayer::PlayingState: mediaPlayer->pause(); break; default: mediaPlayer->play(); break; } }); connect(mediaPlayer, &QMediaPlayer::stateChanged, [m_playButton, this](QMediaPlayer::State state) { switch(state) { case QMediaPlayer::PlayingState: m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause)); break; default: m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay)); break; } }); QSlider *m_positionSlider = new QSlider(Qt::Horizontal,this); m_positionSlider->setRange(0,mediaPlayer->duration() / 1000); connect(mediaPlayer, &QMediaPlayer::positionChanged ,[m_positionSlider, this](qint64 position){ m_positionSlider->setValue(position); }); connect(mediaPlayer, &QMediaPlayer::durationChanged ,[m_positionSlider, this](qint64 duration){ m_positionSlider->setRange(0,duration); }); connect(m_positionSlider ,&QAbstractSlider::sliderMoved, [mediaPlayer ,this] (int position){ mediaPlayer->setPosition(position); }); auto item = new QListWidgetItem("", listWidget_main); auto widget = new QWidget; auto label = new QLabel(finfo.fileName()); auto vb = new QVBoxLayout; QBoxLayout *controlLayout = new QHBoxLayout; controlLayout->setMargin(0); controlLayout->addWidget(m_playButton); controlLayout->addWidget(m_positionSlider); vb->addWidget(label); vb->addWidget(graphicsView); vb->addLayout(controlLayout); widget->setLayout(vb); widget->setMinimumSize(340, 340); listWidget_main->setItemWidget(item,widget); player.append(mediaPlayer); scene->addItem(videoItem); } }); });
The debugger shows that this part is where it stopped.
connect(mediaPlayer, &QMediaPlayer::positionChanged ,[m_positionSlider, this](qint64 position){ m_positionSlider->setValue(position); }); connect(mediaPlayer, &QMediaPlayer::durationChanged ,[m_positionSlider, this](qint64 duration){ m_positionSlider->setRange(0,duration); });
So I disabled position slider and it works completely fine . But I need to use position slider . I don't know how can I fix it.
-
@Kinesis said in Program crash when it loads videos from multiple directories continuously:
connect(listWidget_dirList, & QListWidget::currentRowChanged,
Are you aware that you do this connect everytime your "button" is clicked?
I guess your "m_positionSlider" was deleted and new one created in the meantime? Where do you actually delete them? I only see where you create new m_positionSlider. Where do you delete "mediaPlayer"? The code you posted looks like you have many memory leaks.
-
@jsulm
Yes I am aware that I do this connect everytime the "button" is clicked."I guess your "m_positionSlider" was deleted and new one created in the meantime? Where do you actually delete them?"
Ans:: This is all the code that I am doing now. I think I haven't delected mediaPlayer.
I also think that my code has memory leak . That's why I am stuck here. -
Hi,
You are continuously creating new objects (QMediaPlayer, QGraphicsView, etc.) without ever deleting them or re-using them. Start by fixing that and your leaks should stop.
-
@Kinesis said in Program crash when it loads videos from multiple directories continuously:
OK, how can I delect or reuse them
Like any other object in C++
delete object;
You need to keep the pointers somewhere to be able to do so.
-
@Kinesis You should delete them when they are not needed anymore
1/8