PDA

View Full Version : QConsoleApplication,aboutToQuit and "signal" do not hit the breakpoints...



TorAn
22nd October 2016, 14:35
Following code, running in qtCreator (Windows 10) in the debug mode, does not reach breakpoints "brakepoint1" or "breakpoint2" upon clicking on "x" console window.
What can be the case?
Thanks!


#include <QCoreApplication>
#include <iostream>
#include <signal.h>

void cleanup(int sig)
{
std::cout << sig << "quitting \n"; // breakpoint 2
}

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QObject::connect(&a, &QCoreApplication::aboutToQuit, [&]() {
std::cout << "about to quit\n"; //breakpoint 1
});
signal(SIGINT, cleanup);
signal(SIGABRT, cleanup);
signal(SIGTERM, cleanup);

std::cout<< "about to run\n";
return a.exec();
std::cout<< "exiting\n";
}

anda_skoa
22nd October 2016, 17:02
Maybe the program is killed with SIGKILL when you remove the terminal it is connected to?

Have you tried if it works if you terminate the program yourself?

Cheers,
_

TorAn
22nd October 2016, 18:38
I think that SIGKILL is SIGTERM in Windows, so if it is true then I am tracking this signal.

No, I did not try to terminate the console programmatically, I know it will work. I am interested in doing proper clean up when the console is terminated via "x"-closure. Do you think there is a way to do it?

anda_skoa
23rd October 2016, 10:19
You'll have to check the Windows documentation, it looks like the program simply gets killed.

Cheers,
_

TorAn
23rd October 2016, 12:58
Right, that's what I do, but still - shouldn't it be a cross-platform Qt-solution that addresses the proper handling of such case? I'll try to post the question in qt forum.

jefftee
23rd October 2016, 18:12
Your question has nothing to do with Qt and is operating system specific (for signal handling).

Edit: Here are the signals I trap on Mac OSX which works great: SIGHUP, SIGINT, SIGQUIT, SIGABRT, and SIGTERM. My guess is your app isn't receiving the aboutToQuit signal because a QCoreApplication::quit() has not been done.

TorAn
23rd October 2016, 21:17
jefftee:

I disagree, it has everything to do with Qt being a cross-platform framework. Trapping the signals was not my first choice, I thought that "aboutToQuit" signal will be fired, and it is not, at least in the code sample that I published. Then I moved to signals.
I am happy to simplify my question: forget signals. Why in this case, when I am closing the console window by clicking on "x", the aboutToQuit signal is not fired? Does the sample that I published work on Mac?

anda_skoa
23rd October 2016, 21:32
I disagree, it has everything to do with Qt being a cross-platform framework.

It is, but it is not changing how operating systems work.
It is just part of processes on these operating systems.



Trapping the signals was not my first choice, I thought that "aboutToQuit" signal will be fired, and it is not, at least in the code sample that I published.

Are you sure it is not fired when the application quits?

Have you called QCoreApplication::quit() and the signal did not get emitted?



Why in this case, when I am closing the console window by clicking on "x", the aboutToQuit signal is not fired?
Apparently the process is killed, no?
How would the process execute code when that happens?

Cheers,
_

jefftee
23rd October 2016, 21:32
You do realize that Qt doesn't abstract *everything* on all OS's, right? Signal handling is one of those things that Qt does not provide any coverage for.

The simple answer to your question is that the aboutToQuit signal is never received because your program has never done a QCoreApplication::quit. Being killed by the OS is not the same as the application shutting down. If you want to have your app cleanup when being killed by closing the window, you'll have to handle the correct signal, which my guess is SIGKILL.

You mention that you *think* SIGKILL is the same as SIGTERM, but it would be easy enough for you to try SIGKILL to see for certain.

Edit: A quick google shows that the signal you are looking for is SIGBREAK. Give that a try.

TorAn
24th October 2016, 10:18
jefftee:

First of all, thank you very much for the help! Yes, SIGBREAK worked.
Now, to the points that you mentioned:

SIGKILL is not available on windows, at least I did not find it in signal.h, that's why I mention the possibility that SIGTERM might be the same thing.

But more importantly no, I absolutely did not realize that Qt does not handle the "abnormal" termination. I tried to use "aboutToQuit" because, as documentation states:

This signal is emitted when the application is about to quit the main event loop, e.g. when the event loop level drops to zero. This may happen either after a call to quit() from inside the application or when the users shuts down the entire desktop session. The signal is particularly useful if your application has to do some last-second cleanup.
I thought that it applies to the termination of the console window.

jefftee
25th October 2016, 00:09
Glad you got it working. My interpretation of the aboutToClose documentation is for a normal (clean) shutdown of your app. i.e. your app calls QCoreApplication::quit() or when the OS informs your app that it's shutting down (or logging off, etc). Those would be considered orderly shutdowns of your console app, but clicking the X on the console window, terminating through the task manager, or kill command all would not result in your app going through normal shutdown, etc.