I have created a program that is supposed to run a process over and over again, so that the program is restarted as soon as it it finished. Here is the code that handles that:

Qt Code:
  1. // From process_handler.h
  2. class process_handler : public QObject
  3. {
  4. Q_OBJECT
  5.  
  6. public:
  7. process_handler();
  8.  
  9. private:
  10.  
  11. public slots:
  12. void start_process();
  13.  
  14. private slots:
  15. void process_state_changed();
  16. };
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. // From process_handler.cpp
  2. process_handler::process_handler()
  3. {
  4. connect(&p, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(process_state_changed()));
  5. }
  6.  
  7. void process_handler::process_state_changed()
  8. {
  9. if (p.state() == QProcess::Starting) {
  10. cout << endl;
  11. cout << "Process is starting up..." << endl;
  12. }
  13. if (p.state() == QProcess::Running) {
  14. cout << "Process is now running." << endl;
  15. }
  16. if (p.state() == QProcess::NotRunning) {
  17. cout << "Process is finished running." << endl;
  18.  
  19. /* Start a new process */
  20. //start_process();
  21. QTimer::singleShot(0, this, SLOT(start_process()));
  22. }
  23. }
  24.  
  25. void process_handler::start_process()
  26. {
  27. p.start("program");
  28. }
To copy to clipboard, switch view to plain text mode 

"program" is a program that takes a few seconds to run and thex terminates. As you see I have used a QTimer::singleShot to call start_process() from process_state_changed(). If I don't use it, but call start_process() directly, the text

Qt Code:
  1. Process is starting up...
  2. Process is now running.
  3. Process is finished running.
  4.  
  5. Process is starting up...
  6. Process is now running.
To copy to clipboard, switch view to plain text mode 

is written to cout and then I get a segmentation fault. Does anyone know why this segmentation fault occurs? I would like it if I didn't have to use a singleShot, since that will make coding much more complicated later on.