Qt QTimer is it safe to stop it this way?

Viewed 14948

Is it safe to stop Qt's timer in it's "timeout" signal/slot function? Can't seem to find any information in Qt documentation about the QTimer.

I have created a timer that is periodically sending a "keep alive" messages to the server. I want this timer to be stopped if there is some kind of error while sending my message.

private:
   QTimer* mpKeepAliveTimer;

Timer is initialized like this:

mpKeepAliveTimer = new QTimer(/* this */);

QObject::connect(mpKeepAliveTimer, SIGNAL(timeout()), this, SLOT(OnKeepAlive()));

mpKeepAliveTimer->start(KEEP_ALIVE_PERIOD);

Stopped like this:

if (mpKeepAliveTimer != NULL) // <-- Edited
{
    if (mpKeepAliveTimer->isActive() == true)
        mpKeepAliveTimer->stop();

    delete mpKeepAliveTimer;
    mpKeepAliveTimer = NULL;
}

Timeout function looks like this:

void Classname::OnKeepAlive()
{
   if (isErrorFound == true)
      mpKeepAliveTimer->stop();   // <---- IS THIS SAFE?
}

Thanks.

1 Answers
Related