Potential memory leak in QMediaPlayer.
Unsolved
Qt 6
-
Hi, I'm trying to learn Qt and make a music player. The first function I'm trying to implement is scanning track for metadata. Currently, Qt doesn't provide direct method to extract media metadata and I have to use QMediaPlayer. However, I noticed that after few hundred setSource() the program grew in size considerately. And after almost 2000 songs, the program occupied 200MB more memory than initially.
So, I wonder if that is how it supposes to work or it's a memory leak issue. This was test on Linux, have not tested on Windows.
The code below is short example of what I'm trying to achieve. There is nothing (at least to my knowledge) saved except for the file paths in queue.// main.h #ifndef MAIN_H #define MAIN_H #include <QAudioOutput> #include <QDirIterator> #include <QFileInfo> #include <QMediaMetaData> #include <QMediaPlayer> #include <QMimeDatabase> #include <QMimeType> #include <QQueue> class TestClass : public QObject { Q_OBJECT public: TestClass(QObject *parent) : QObject(parent), mediaPlayer(this) { mediaPlayer.setAudioOutput(new QAudioOutput(&mediaPlayer)); connect(&mediaPlayer, &QMediaPlayer::mediaStatusChanged, this, [=](QMediaPlayer::MediaStatus status) { if (status == QMediaPlayer::LoadedMedia) { // Read metadata if the file is loaded if (mediaPlayer.error() == QMediaPlayer::NoError) { qDebug() << mediaPlayer.metaData().stringValue(QMediaMetaData::Url); } mediaPlayer.setSource(QUrl()); // this does nothing running = false; run(); } }); }; void addQueue(QString path) // scan path for audio file { QDirIterator iterator(path, QDirIterator::FollowSymlinks | QDirIterator::Subdirectories); while (iterator.hasNext()) { QFileInfo fileInfo(iterator.next()); if (fileInfo.isReadable() && fileInfo.isFile()) { QMimeType mime = QMimeDatabase().mimeTypeForFile(fileInfo); if (mime.name().contains("audio/")) { // only queue up audio file queue.enqueue(fileInfo.absoluteFilePath()); } } } }; void run() { while (!running && !queue.isEmpty()) { running = true; mediaPlayer.setSource(queue.dequeue()); } } private: bool running = false; QMediaPlayer mediaPlayer; QQueue<QString> queue; }; #endif // MAIN_H
// main.cpp #include <QCoreApplication> #include "main.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); TestClass testClass(&a); testClass.addQueue("insert your path here"); testClass.run(); return a.exec(); }