PDA

View Full Version : Memory leak with QTimer?



jano_alex_es
8th January 2010, 15:02
Hi, I have the next code, as far as I've seen, pretty similar to the normal usage of QTimer.


QTimer* pAuxTimer = new QTimer();
pAuxTimer->setSingleShot(true);
QSignalMapper* mapper = new QSignalMapper();
connect(pAuxTimer, SIGNAL(timeout()), mapper, SLOT(map()));
connect(mapper, SIGNAL(mapped(QWidget*)), this, SLOT(showTranspAdvertisement(QWidget*)));
mapper->setMapping(pAuxTimer, pAuxWid);
pAuxTimer->start(50);

It works fine, but it creates a memory leak because that's the end of the function. I can use a non local variable because the code is executed when the user presses a button, so I don't know how many times is gonne be executed, I don't know how many timers I'm gonna have working at the same time...

Does Qt eliminate them automatically? docs says nothing about that.

thansk!

wysota
8th January 2010, 23:00
If you provide a parent to the timer through its constructor then Qt will delete the timer once the parent is deleted.