Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. Potential memory leak in QMediaPlayer.
QtWS25 Last Chance

Potential memory leak in QMediaPlayer.

Scheduled Pinned Locked Moved Unsolved Qt 6
1 Posts 1 Posters 319 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TheBill16
    wrote on last edited by
    #1

    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();
    }
    
    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved