Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8381571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:43:36+00:00 2026-06-09T16:43:36+00:00

Is it safe to stop Qt’s timer in it’s timeout signal/slot function? Can’t seem

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:43:37+00:00Added an answer on June 9, 2026 at 4:43 pm

    As long as you are not explicitly using Queued Connections, this is safe.
    This is because the emit timeout() function will not return until all the slots it’s connected to were processed.

    If you were however using Queued Connections, it could in theory happen that there are still unprocessed timeout events in the Event Queue, so to make it hyper-safe you could use the following:

    void Classname::OnKeepAlive()
    {
        if (!mpKeepAliveTimer || !mpKeepAliveTimer->isActive()) return;
    
        if (isErrorFound)
        {
            mpKeepAliveTimer->stop();
        }
    }
    

    Note that the condition in your stop function should be != NULL instead of == NULL. You can also write that function as follows, however:

    if (mpKeepAliveTimer)
    {
        delete mpKeepAliveTimer;
        mpKeepAliveTimer = NULL;
    }
    

    As already suggested in the comments, QTimer will stop itself in its destructor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know it's safe to draw on any thread so long as I call
Is there any safe and standard compliant way to treat a C style array
Is the following code safe? boost::any any_value; { std::string s = HelloWorld; any_value =
I'm pretty much positive about this, but just to be on the safe side:
I'm refactoring some time consuming function so that it can be called from a
Is there any better way to stop spam coming through on my phpmailer script?
I recently read this article Safe Thread Synchronization as I was curious about the
can I do this? [array enumerateUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){ if (idx >
What's the best one-stop-shop safe C library solution on the Mac? I use quotes
This is a follow up question to How can I stop an IFrame reloading

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.