Solved Parent process, child process and QCoreApplication
-
@SPlatten said in Parent process, child process and QCoreApplication:
Still doesn't work
So, did you actually check whether the onExitModule slot was called?
-
@SPlatten said in Parent process, child process and QCoreApplication:
Still doesn't work.
What does not work?
The slot is not called or the instance does not stop? -
@jsulm , yes and it isn't.
-
@SPlatten than I would assume your application doesn't actually quit :P
call quit manually in a timer after x amount of seconds, to see if everything closes correctly
-
@SPlatten said in Parent process, child process and QCoreApplication:
According to the documentation, using start will cause the child process to close when the parent is closed, which is exactly the functionality I want. It did work then I think after I modified the child to use QCoreApplication it has stopped working, in that when the parent process is terminated, the child process keeps running.
I'm going to throw my 2 cents in. Child process exiting when parent process exits --- so long as not detached, and child does not do something in its start up code to prevent it --- is an OS thing, not a Qt/C++ thing. So I would expect it to exit when parent exits, regardless of
QCoreApplication
or otherwise.[*]Are you sure (I mean 100%, guaranteed, you'll stake your grandmother on it) your parent process is exiting? For example, if it doesn't have a UI you won't get the default "exit app on last window closed" behaviour....
[*] EDIT I'm rethinking my claim/stance on this, OS-wise. It's more complex than I recalled. Luckily, I don't have any grandmother to stake on this statement, though I may be 2 cents poorer....
-
@JonB , It doesn't the process was launched using QProcess and the start method, I stopped the process that launched the child by closing all the windows. I then checked the processes running using:
ps -A
I can see the child is still running. I also attached to the child process using Qt Creator and put a break point in the onExitModule slot, it never gets there.
-
@SPlatten said in Parent process, child process and QCoreApplication:
I stopped the process that launched the child by closing all the windows.
I don't understand. You said it's now a
QCoreApplication
...Oh, it's the child which is the
QCoreApplication
, not the parent?I stopped the process that launched the child by closing all the windows
Maybe. Nonetheless, verify that the parent really does exit? Verify its (the parent's) PID has gone?
I am lost. Could you please make clear which process(es) are
QCoreApplication
s and which areQApplication
s, out of your parent & children? Just simply & clearly.Now that I have withdrawn/reneged on my earlier post claiming that OS will terminate children on parent exit:
If your parent is a UI application, you should be able to get its
aboutToQuit()
, and forcibly kill any child processes. Why you should need to do that when you usedQProcess::start()
I'm not sure. -
@JonB To clarify, the child application main:
int main(int intArgc, char* parystrArgv[]) { QCoreApplication a(intArgc, parystrArgv); QCoreApplication::setApplicationName(clsModFileIO::scpszTitle()); clsModFileIO obj(&a, intArgc, parystrArgv); return a.exec(); }
The constructor for clsModFileIO:
clsModHelper::clsModHelper(QObject* pParent, int intArgc, char* parystrArgv[] ,const char* cpszTitle, double dblVersion) : QTcpSocket(pParent) , mdblVersion(dblVersion) , mfpDbgLog(nullptr) , mint64AppPID(QCoreApplication::applicationPid()) , mstrTitle(cpszTitle) , muint16ModulePort(0), muint16LauncherPID(0) , muint16XMLMPAMport(0) { if ( intArgc < CLA_LAUNCHER_PID ) { std::cout << "Insufficient arguments, aborting!" << std::endl; exit(EXIT_FAILURE); } if ( mspThis == nullptr ) { mspThis = this; } muint16XMLMPAMport = (quint16)atoi(parystrArgv[CLA_XMLMPAM_PORT]); muint16ModulePort = (quint16)atoi(parystrArgv[CLA_MODULE_PORT]); muint16LauncherPID = (quint16)atoi(parystrArgv[CLA_LAUNCHER_PID]); setSocketOption(QAbstractSocket::LowDelayOption, 1); //Connect up the signals QObject::connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit ,this, &clsModHelper::onExitModule); ...
-
@SPlatten
Your reply may have crossed with my update.-
I can now see that your child processes are clearly
QCoreApplication
. -
What is your parent application?
QApplication
orQCoreApplication
?
EDIT
No, no, what's this://Connect up the signals QObject::connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit ,this, &clsModHelper::onExitModule);
That is "about to quit" in the child. I thought you said you want to the parent to exit, and the children should exit at the same time, isn't that your whole question?
-
-
@JonB Parent application main:
QApplication a(argc, argv);
-
@SPlatten
Did you see my EDIT in previous post? -
@JonB Thats in the child process, sorry, just read the whole edit. What I want is the child process to automatically close when the parent is terminated.
-
@SPlatten
I can see yourconnect(aboutToQuit)
is in the child process. That's the wrong place if you are saying:-
You have a parent process (UI
QApplication
). -
You have that spawn some child processes, via
QProcess::start()
. -
You exit the parent (perhaps you close all its windows)...
-
... And you want the previously-spawned child processes to exit when the parent does.
Can't you just say if that is what you are asking?
EDIT Ah, OK, I think you have edited to say that is indeed what you want.
OK then, you are putting the code in the wrong application :)
Supposedly, you shouldn't have to do anything.
QProcess::start()
looks like it should do that, as a result of what it says forstartDetached()
:If the calling process exits, the detached process will continue to run unaffected.
I think you may have said at one point the children were exiting when the parent did. And then I think you say you happened to change the children to
QCoreApplication
, and, for whatever reason, they ceased exiting when the parent does?Anyway. If your children are not exiting and you want them to do so when exiting the parent, we are (at least I am) suggesting you forcibly terminate them from the parent just before it exits. So, in the parent, keep a list of the created, non-exited child
QProcess
es. Catch the aboutToQuit or whatever signal in the parent, make thatQProcess::terminate()
(or failing thatQProcesss::kill()
) the children just before exiting the parent.Is that what you need?
Separately. IF your parent/children are server/clients across sockets to each other --- as I thought they were --- the server should be able to send a message and close the sockets to the clients, and they should exit on that, instead of terminating processes.
-