Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Playing audio using thread
Forum Updated to NodeBB v4.3 + New Features

Playing audio using thread

Scheduled Pinned Locked Moved Solved General and Desktop
25 Posts 5 Posters 5.9k Views 5 Watching
  • 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.
  • Kent-DorfmanK Offline
    Kent-DorfmanK Offline
    Kent-Dorfman
    wrote on last edited by Kent-Dorfman
    #4

    It may or may not be related but I found leaks last year in the Qt multimedia module (gstreamer as default backend) where it did not close URL based video streams when I would switch between multiple video feeds. The result was that after 20 or 30 stream switches it would quit working. It's possible and probable that this resource leak still exists.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goshantry
      wrote on last edited by
      #5

      I tried using valgrind from qt to check for the memory leaks and the program crashes as soon as it starts. Also i tried to use it through terminal and it shows no leaks and errors.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #6

        @Kent-Dorfman Any chance of remembering the version you used ?

        @goshantry What version of Qt are you using ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        G 1 Reply Last reply
        -1
        • Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on last edited by
          #7

          I still have the project directory so you are in luck:

          From the Makefile Generated by qmake (3.1) (Qt 5.9.4)

          The app was an operator console for an unmanned vehicle. It had four IP cameras and cycling through the feeds or toggling the same feed on and off many times eventually caused the feed to die.

          1 Reply Last reply
          0
          • SGaistS SGaist

            @Kent-Dorfman Any chance of remembering the version you used ?

            @goshantry What version of Qt are you using ?

            G Offline
            G Offline
            goshantry
            wrote on last edited by
            #8

            @SGaist I am using Qt creator 4.8.1

            jsulmJ 1 Reply Last reply
            0
            • G goshantry

              @SGaist I am using Qt creator 4.8.1

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #9

              @goshantry said in Playing audio using thread:

              I am using Qt creator 4.8.1

              The question was what Qt version you're using, not which QtCreator version.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              G 1 Reply Last reply
              0
              • jsulmJ jsulm

                @goshantry said in Playing audio using thread:

                I am using Qt creator 4.8.1

                The question was what Qt version you're using, not which QtCreator version.

                G Offline
                G Offline
                goshantry
                wrote on last edited by
                #10

                @jsulm Sorry for that
                I am using Qt 5.12.0

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #11

                  Can you check with Qt 5.12.1 ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  -1
                  • G Offline
                    G Offline
                    goshantry
                    wrote on last edited by
                    #12

                    checked with Qt 5.12.1
                    I am still facing the same error

                    jsulmJ 1 Reply Last reply
                    0
                    • G goshantry

                      checked with Qt 5.12.1
                      I am still facing the same error

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #13

                      @goshantry Is it working properly without threads?

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        goshantry
                        wrote on last edited by
                        #14

                        After much hit and trial I found that when the program goes into the mainTimerCallback function, it initializes audObj every time. This object gradually takes up the memory. If i block this function my code works fine.

                        Any idea as to why this object does not free the memory as soon as this function is completed?

                        I tried doing this without QThread and everything works fine

                        I also used audObj.deletelater() function but the system shows the same error

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #15

                          Because it's not how memory manager works necessarily.

                          By the way, with the code you shown, your QThread doesn't do anything as you call the run method explicitly except for the one in your main.cpp file.

                          Also note that your mutex isn't useful as it's local to the run method so it's created each time the method is called.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          1
                          • G Offline
                            G Offline
                            goshantry
                            wrote on last edited by
                            #16

                            Is there any way to free up memory after an object has been created in Qt or does it automatically frees up memory after the function has been completed?

                            jsulmJ 1 Reply Last reply
                            0
                            • G goshantry

                              Is there any way to free up memory after an object has been created in Qt or does it automatically frees up memory after the function has been completed?

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #17

                              @goshantry Variables allocated on the stack are freed automatically when the function/method finishes.
                              If a variable was allocated on the heap (new) then it is either freed when the parent is deleted (if you're using Qt parent/child feature) or you have to delete it at some point.
                              As @SGaist pointed out your mutex is useless the way you're using it...

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              G 1 Reply Last reply
                              1
                              • jsulmJ jsulm

                                @goshantry Variables allocated on the stack are freed automatically when the function/method finishes.
                                If a variable was allocated on the heap (new) then it is either freed when the parent is deleted (if you're using Qt parent/child feature) or you have to delete it at some point.
                                As @SGaist pointed out your mutex is useless the way you're using it...

                                G Offline
                                G Offline
                                goshantry
                                wrote on last edited by
                                #18

                                @jsulm I removed that part of my code and created a new function to play audio without thread. Everything works fine and I left to test it for 2 days. Now when I run the free -m command in the terminal it shows only around 250-300 mb remaining out of my 8Gb. The swap memory is also used to a great extent.

                                Is this memory because of those objects which I declared with new() or with any object that has been created?

                                Also do I use the delete later with every object that i created at the end of the function to release the memory?

                                My application is supposed to run for long hours without having to shutdown or restart the system.

                                jsulmJ 1 Reply Last reply
                                0
                                • G goshantry

                                  @jsulm I removed that part of my code and created a new function to play audio without thread. Everything works fine and I left to test it for 2 days. Now when I run the free -m command in the terminal it shows only around 250-300 mb remaining out of my 8Gb. The swap memory is also used to a great extent.

                                  Is this memory because of those objects which I declared with new() or with any object that has been created?

                                  Also do I use the delete later with every object that i created at the end of the function to release the memory?

                                  My application is supposed to run for long hours without having to shutdown or restart the system.

                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #19

                                  @goshantry You release the memory when you don't need it anymore.
                                  "Also do I use the delete later with every object that i created at the end of the function to release the memory?" - do you mean deleteLater() method?
                                  You should use Valgrind to analyse where your app is consuming memory.
                                  "Is this memory because of those objects which I declared with new() or with any object that has been created?" - any of course. Everything in use by your app.
                                  Can you show the code you're currently using?

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  G 1 Reply Last reply
                                  0
                                  • jsulmJ jsulm

                                    @goshantry You release the memory when you don't need it anymore.
                                    "Also do I use the delete later with every object that i created at the end of the function to release the memory?" - do you mean deleteLater() method?
                                    You should use Valgrind to analyse where your app is consuming memory.
                                    "Is this memory because of those objects which I declared with new() or with any object that has been created?" - any of course. Everything in use by your app.
                                    Can you show the code you're currently using?

                                    G Offline
                                    G Offline
                                    goshantry
                                    wrote on last edited by goshantry
                                    #20

                                    @jsulm I tried using valgrind in Qt and this is the output which I get everytime

                                    11:40:37: valgrind --child-silent-after-fork=yes --xml-socket=127.0.0.1:43171 --log-socket=127.0.0.1:46593 --xml=yes --smc-check=stack --tool=memcheck --gen-suppressions=all --track-origins=yes --leak-check=summary --num-callers=25 /home/fiem/workspace/build-PAPIS-Desktop_Qt_5_12_1_GCC_64bit-Debug/PAPIS
                                    11:41:11: The program has unexpectedly finished.
                                    11:41:11: Process exited with return value Process crashed
                                    11:41:11: Analyzing finished.
                                    

                                    Also I changed the Audio Playing function to

                                    // Constructor for class
                                    pisUser::pisUser(QWidget *parent) :
                                        QWidget(parent),
                                        ui(new Ui::pisUser)
                                    {
                                        ui->setupUi(this);
                                        player = new QMediaPlayer(this);
                                    }
                                    
                                    // Function to play audio
                                    void pisUser::playAudioDirect()
                                    {
                                    //    player->deleteLater();
                                        player->setMedia(QUrl::fromLocalFile(path));
                                        player->play();               // Play the audio sounds
                                    }
                                    

                                    also player->deleteLater(); always crashes the application no matter where i put it

                                    jsulmJ 1 Reply Last reply
                                    0
                                    • G goshantry

                                      @jsulm I tried using valgrind in Qt and this is the output which I get everytime

                                      11:40:37: valgrind --child-silent-after-fork=yes --xml-socket=127.0.0.1:43171 --log-socket=127.0.0.1:46593 --xml=yes --smc-check=stack --tool=memcheck --gen-suppressions=all --track-origins=yes --leak-check=summary --num-callers=25 /home/fiem/workspace/build-PAPIS-Desktop_Qt_5_12_1_GCC_64bit-Debug/PAPIS
                                      11:41:11: The program has unexpectedly finished.
                                      11:41:11: Process exited with return value Process crashed
                                      11:41:11: Analyzing finished.
                                      

                                      Also I changed the Audio Playing function to

                                      // Constructor for class
                                      pisUser::pisUser(QWidget *parent) :
                                          QWidget(parent),
                                          ui(new Ui::pisUser)
                                      {
                                          ui->setupUi(this);
                                          player = new QMediaPlayer(this);
                                      }
                                      
                                      // Function to play audio
                                      void pisUser::playAudioDirect()
                                      {
                                      //    player->deleteLater();
                                          player->setMedia(QUrl::fromLocalFile(path));
                                          player->play();               // Play the audio sounds
                                      }
                                      

                                      also player->deleteLater(); always crashes the application no matter where i put it

                                      jsulmJ Offline
                                      jsulmJ Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #21

                                      @goshantry said in Playing audio using thread:

                                      also player->deleteLater(); always crashes the application no matter where i put it

                                      That's because player has a parent and parent deletes its children when it is deleted, so you have a double delete. Do not pass "this" to player as parent (or do not call delete later).
                                      And why do you try to call deleteLater BEFORE you actually use it?! You should do it when you don't need it anymore.
                                      See https://doc.qt.io/qt-5/objecttrees.html

                                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      1 Reply Last reply
                                      0
                                      • K Offline
                                        K Offline
                                        kuzulis
                                        Qt Champions 2020
                                        wrote on last edited by kuzulis
                                        #22

                                        AFAIK, QtMM is not designed to use in separate threads (look to QMediaPlayer source code).

                                        1 Reply Last reply
                                        0
                                        • G Offline
                                          G Offline
                                          goshantry
                                          wrote on last edited by
                                          #23

                                          I was reading about QPointers and I tried the following

                                          QPointer<QMediaPlayer> player = new QMediaPlayer(this);
                                          
                                              //player = new QMediaPlayer(this);
                                          

                                          the part which I wrote in comments works fine
                                          The part with QPointer crashes my application

                                          It comes in the following function

                                          void QMediaPlayer::setMedia(const QMediaContent &media, QIODevice *stream)
                                          {
                                              Q_D(QMediaPlayer);
                                              stop();
                                          
                                              QMediaContent oldMedia = d->rootMedia;
                                              d->disconnectPlaylist();
                                              d->playlist = 0;
                                              d->rootMedia = media;
                                              d->nestedPlaylists = 0;
                                          
                                              if (oldMedia != media)
                                                  emit mediaChanged(d->rootMedia);
                                          
                                              if (media.playlist()) {
                                                  // reset playlist to the 1st item
                                                  media.playlist()->setCurrentIndex(0);
                                                  d->setPlaylist(media.playlist());
                                              } else {
                                                  d->setMedia(media, stream);
                                              }
                                          }
                                          

                                          I get the segmentation fault error and the program crashes

                                          jsulmJ 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