// c++ -fPIC -lQt6Core -lQt6DBus -L/usr/local/lib/qt6 -I/usr/local/include/qt6 -I/usr/local/include/qt6/QtCore -I/usr/local/include/qt6/QtGui -I/usr/local/include/qt6/QtWidgets -I/usr/local/include/qt6/QtDBus -g #include #include #include #include #include using namespace std; int usage(); void do_fork(); void do_stuff(); int main(int argc, char* argv[]) { if (argc != 3) return usage(); QCoreApplication app(argc, argv); if (string(argv[1]) == "fork") { do_fork(); do_stuff(); } else { do_stuff(); do_fork(); } } void do_stuff() { // this triggers creation of the global static QDBusConnectionManager within Qt // the hang then occurs in QDBusConnectionManager destructor after the child calls exit() QDBusConnection conn("blah"); } void do_fork() { pid_t pid = ::fork(); if (pid < 0) { cout << "fork failed"; return; } // child if (pid == 0) { ::sleep(1); ::exit(0); return; } // parent int status; int r = waitpid(pid, &status, 0); if (r < 0) { cout << "waitpid failed"; return; } } int usage() { cout << "works: ./a.out fork stuff" << endl << "hangs: ./a.out stuff fork" << endl; return 1; }