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 9069833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:31:55+00:00 2026-06-16T17:31:55+00:00

The following piece of code starts a process that takes one second to finish,

  • 0

The following piece of code starts a process that takes one second to finish, and subsequently waits for that process to finish before exiting. For some reason, the following code hangs in p->waitForFinished() even though the process did finish.

#include <QtCore/QProcess>    

class A
{
  public:
    A():
        p(0)
    {
    }

    ~A()
    {
        p->waitForFinished();
        delete p;
    }

    void start()
    {
        p = new QProcess(0);
        p->start("sleep 1");
    }

    QProcess *p;
};

int main(void)
{
  static A a;
  a.start();

  return 0;
}

However, as soon as a is not declared statically, but as follows:

A a;

the waitForFinished() call succeeds. Is this a Qt bug, or is this expected behavour? My suspicion is that some kind of logic required to detect whether an application successfully finished is already destroyed once the destructor of A is called.

  • 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-16T17:31:57+00:00Added an answer on June 16, 2026 at 5:31 pm

    You have tried to clean up the thread created by QProcess two different ways at the same time, so this is a bug in your program.

    You’ve detached the thread by returning from main (which terminates all threads in the process, detaching them if they are joinable).

    And you’ve cleaned up the QProcess thread by joining it through waitForFinished.

    You can detach a thread or you can join it, but you can’t do both, even indirectly. Apparently, the detach wins, and the join hangs.

    Most likely this is because QProcess uses its own termination signal rather than the one built into the threading library. So the return from main terminates the thread before it can send that termination signal, leaving the waitForFinished function to wait for a signal that will never be sent.

    As a general rule, threads should not be created in constructors nor cleaned up in destructors. This is primarily because the timing of these operations requires more explicit control than is possible. And they should never be created before main starts nor cleaned up after main returns — again because you need to control the context in which these things happen.

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

Sidebar

Related Questions

I have the following piece of code that takes in some words, stores them
The following piece of code doesn't make an application to run. Can some one
The following piece of code runs fine when parallelized to 4-5 threads, but starts
the following piece of code works fine when i use it among some code
I have another newbie Python question. I have the following piece of code that
I seem to have created an immortal process with the following piece of code:
I have the following piece of code that I am having problems resetting the
I have a following piece of code (.net 4) that is consuming a lot
The following piece of code is used to print the time in the logs:
The following piece of code should read each line of the file and operate

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.