Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. [Ask] What happen if QTimer doesn't stop
Forum Updated to NodeBB v4.3 + New Features

[Ask] What happen if QTimer doesn't stop

Scheduled Pinned Locked Moved Solved Mobile and Embedded
11 Posts 4 Posters 2.8k Views 1 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.
  • H Offline
    H Offline
    Hung Tran
    wrote on last edited by
    #1

    Hi,

    I have a question that: How about memory or something else happen if I start a QTimer but don't stop it.

    This is my code:

    File a.h

    //your code here
    ```void updateProgressBar()
    
    File a.cpp
    void DivXPlayer::updateProgressBar(){
        timer= new QTimer(this);
        timer->setInterval(1000);
        timer->setSingleShot(false);
        connect(timer,SIGNAL(timeout()),
                myMainUi,
                SLOT(setProgressBar()));
        timer->start();
    }
    
    file c.cpp
    
    void call(){
    while(1){
         updateProgressBar();
      }
    }
    
    
    Thank so much.
    jsulmJ 1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You are creating new QTimer objects in a tight, unending loop. So, my bet is you're going to run out of memory pretty quickly.

      Regarding your question, though: well, if the timer is repeating (and it is), it will continue running and emit "timeout()" signal every second (triggering the setTProgressBar() slot) until you close your application.

      (Z(:^

      1 Reply Last reply
      2
      • H Hung Tran

        Hi,

        I have a question that: How about memory or something else happen if I start a QTimer but don't stop it.

        This is my code:

        File a.h

        //your code here
        ```void updateProgressBar()
        
        File a.cpp
        void DivXPlayer::updateProgressBar(){
            timer= new QTimer(this);
            timer->setInterval(1000);
            timer->setSingleShot(false);
            connect(timer,SIGNAL(timeout()),
                    myMainUi,
                    SLOT(setProgressBar()));
            timer->start();
        }
        
        file c.cpp
        
        void call(){
        while(1){
             updateProgressBar();
          }
        }
        
        
        Thank so much.
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Hung-Tran To add to @sierdzio : the timer itself will not consume more memory just because it isn't stopped. But if the slot connected to it allocates memory without freeing it then you will consume more and more memory, but not because of the timer.

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

        H 1 Reply Last reply
        4
        • jsulmJ jsulm

          @Hung-Tran To add to @sierdzio : the timer itself will not consume more memory just because it isn't stopped. But if the slot connected to it allocates memory without freeing it then you will consume more and more memory, but not because of the timer.

          H Offline
          H Offline
          Hung Tran
          wrote on last edited by
          #4

          @jsulm
          Connected Slot just set new value for a label and does not allocate anything. So how about memory in this case ?

          jsulmJ E 2 Replies Last reply
          0
          • H Hung Tran

            @jsulm
            Connected Slot just set new value for a label and does not allocate anything. So how about memory in this case ?

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

            @Hung-Tran "So how about memory in this case?" - it was already explained. QTimer itself will not increase its memory consumption if not stopped. But as @sierdzio said: you're creating new QTimers in an endless loop! This will eat up all your memory quite fast, but this is not QTimer issue, but your fault.

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

            H 1 Reply Last reply
            2
            • H Hung Tran

              @jsulm
              Connected Slot just set new value for a label and does not allocate anything. So how about memory in this case ?

              E Offline
              E Offline
              Eligijus
              wrote on last edited by
              #6

              @Hung-Tran You are allocating memory with this line

              timer= new QTimer(this);
              

              Operator "new" is used to allocate memory http://en.cppreference.com/w/cpp/memory/new/operator_new

              1 Reply Last reply
              0
              • jsulmJ jsulm

                @Hung-Tran "So how about memory in this case?" - it was already explained. QTimer itself will not increase its memory consumption if not stopped. But as @sierdzio said: you're creating new QTimers in an endless loop! This will eat up all your memory quite fast, but this is not QTimer issue, but your fault.

                H Offline
                H Offline
                Hung Tran
                wrote on last edited by
                #7

                @jsulm I got it. Because the value I want to update is position of a playing clip. So I want to update every second to update progress bar. So do you have solution in my case to update position of clip every second ?

                H jsulmJ sierdzioS 3 Replies Last reply
                0
                • H Hung Tran

                  @jsulm I got it. Because the value I want to update is position of a playing clip. So I want to update every second to update progress bar. So do you have solution in my case to update position of clip every second ?

                  H Offline
                  H Offline
                  Hung Tran
                  wrote on last edited by
                  #8

                  @Hung-Tran Thank. I got it.

                  1 Reply Last reply
                  1
                  • H Hung Tran

                    @jsulm I got it. Because the value I want to update is position of a playing clip. So I want to update every second to update progress bar. So do you have solution in my case to update position of clip every second ?

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

                    @Hung-Tran said in [Ask] What happen if QTimer doesn't stop:

                    So do you have solution in my case to update position of clip every second ?

                    Yes, use QTimer.

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

                    1 Reply Last reply
                    2
                    • H Hung Tran

                      @jsulm I got it. Because the value I want to update is position of a playing clip. So I want to update every second to update progress bar. So do you have solution in my case to update position of clip every second ?

                      sierdzioS Offline
                      sierdzioS Offline
                      sierdzio
                      Moderators
                      wrote on last edited by
                      #10

                      @Hung-Tran said in [Ask] What happen if QTimer doesn't stop:

                      So do you have solution in my case to update position of clip every second ?

                      You already have a soltion:

                      void DivXPlayer::updateProgressBar(){
                          timer= new QTimer(this);
                          timer->setInterval(1000);
                          timer->setSingleShot(false);
                          connect(timer,SIGNAL(timeout()),
                                  myMainUi,
                                  SLOT(setProgressBar()));
                          timer->start();
                      }
                      

                      This method is OK. You just need to call it once, not in an endless loop!

                      And if you want to stop the timer after reaching 100%, either stop it from within setProgressBar(), or add a check for that in your DivXPlayer class, for example.

                      (Z(:^

                      H 1 Reply Last reply
                      2
                      • sierdzioS sierdzio

                        @Hung-Tran said in [Ask] What happen if QTimer doesn't stop:

                        So do you have solution in my case to update position of clip every second ?

                        You already have a soltion:

                        void DivXPlayer::updateProgressBar(){
                            timer= new QTimer(this);
                            timer->setInterval(1000);
                            timer->setSingleShot(false);
                            connect(timer,SIGNAL(timeout()),
                                    myMainUi,
                                    SLOT(setProgressBar()));
                            timer->start();
                        }
                        

                        This method is OK. You just need to call it once, not in an endless loop!

                        And if you want to stop the timer after reaching 100%, either stop it from within setProgressBar(), or add a check for that in your DivXPlayer class, for example.

                        H Offline
                        H Offline
                        Hung Tran
                        wrote on last edited by
                        #11

                        @sierdzio Thank you. I got it. I'll change its behavior in my code.

                        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