Unsolved Small example showing QTimer slowing to a halt
-
Hello.
I managed to isolate my bug down to a very small test case, and was wondering whether I'm doing something wrong, or whether QT has a bug.
I'm trying to set up a QTimer that just prints "tick" every 33ms. When I ran the program, it starts off doing that fine. After running for about 2 minutes, the ticks slow down to about once every 10 seconds.
I feel I'm hitting a memory-leak, or some buffer is getting really long and I'm not clearing it.
This is QT4.8 running on Mac OS-X 10.11.6.
Thanks for your help!
-PatrickIn test.h:
#ifndef TEST_H #define TEST_H #include <QObject> class Driver : public QObject{ Q_OBJECT public: Driver(); public slots: void tick(); }; #endif
In test.cpp:
#include "test.h" #include <iostream> #include <QApplication> #include <QTimer> using namespace std; Driver::Driver(){} void Driver::tick(){ cout << "tick" << endl; } QApplication* app; Driver* driver; QTimer* timer; int main(int argc, char** argv){ app = new QApplication(argc, argv); app->setQuitOnLastWindowClosed(false); driver = new Driver; timer = new QTimer; timer->setInterval(33); QObject::connect(timer, SIGNAL(timeout()), driver, SLOT(tick())); timer->start(); return app->exec(); }
-
Hello @CuppoJava
Your code is perfectly fine. Didn't find any issue with memory leak. Maybe this Qt version contain bug for Mac OS. -
Hi and welcome to devnet,
What version of Qt 4.8 ?
By the way, there's no need for all these static variables that you are using especially app.
int main(int argc, char** argv){ QApplication app(argc, argv); app.setQuitOnLastWindowClosed(false); Driver driver; QTimer timer; timer.setInterval(33); QObject::connect(&timer, SIGNAL(timeout()), &driver, SLOT(tick())); timer.start(); return app.exec(); }
That way you don't have any memory leaks.
On a side note, Qt 4.8 has reached end of line since a couple of years now, so if possible, you should rather start your new projects with Qt 5.
-
Thanks very much for the help. I'll upgrade to QT5 to see whether that solves it. I'm working on a client project right now that's written in QT4, so if it turns out to be a bug in QT I'll have to find a workaround.
-
Just as an update, I upgraded to QT5 and the problem is still there. At this point, I almost think that it's a bug with the OSX terminal, except that I can't reproduce the problem using anything except QTimer. Does anyone have any suspicions that I can use as a starting point?
Thanks very much!
-Patrick -
I would put an QElapsedTimer in the driver and look at the actual interval.
If the actual interval is still at the expected time, but the display is off, then
its the terminal not being able to keep up. -
@mranger90 said in Small example showing QTimer slowing to a halt:
I would put an QElapsedTimer in the driver and look at the actual interval.
If the actual interval is still at the expected time, but the display is off, then
its the terminal not being able to keep up.That's a great point, forcing a clear on the terminal may "reset" the delay
-
That is a great tip. Thanks I'll run that experiment!
-
Okay. I ran the experiment. I started a QElapsedTimer at the same time as my QTimer (which is set to an interval of 33 ms) and printed out the elapsed time between each call. And it prints out the following, which shows a steady interval of 33ms followed by a sudden period where the interval jumps up to about 10s.
So I guess this rules out the OSX terminal as the cause of the slowdown. I verified that I see the same behaviour using both QT4 and QT5. Do you guys have any other ideas? I'm completely stumped.
Thanks!
-Patrick[...] tick 889 elapsed time: 33 tick 890 elapsed time: 33 tick 891 elapsed time: 32 tick 892 elapsed time: 32 tick 893 elapsed time: 32 tick 894 elapsed time: 32 tick 895 elapsed time: 32 tick 896 elapsed time: 10033 tick 897 elapsed time: 8457 tick 898 elapsed time: 4837 tick 899 elapsed time: 38 tick 900 elapsed time: 10030 tick 901 elapsed time: 10032 tick 902 elapsed time: 10031 [...]