October 13, 2010

s.frings s.frings
Lab Rat
40 posts

Destructors not called when I terminate a console app with Ctrl-C

 

Hello,
on windows, when I enter Ctrl-C while any console app is running, QT-Creator shows a negative exit code, for example -1073741510.

On Linux, I also see a negative return code plus a remark that the application finished unexpectedly. If I set the GUI language to german it says that the application crashed (in german words).

Is that normal or a technical problem? Example source to reproduce:

  1. int main(int argc, char *argv[]) {
  2.     QCoreApplication app(argc, argv);
  3.     return app.exec();
  4. }

I think, it’s fairly impossible to make a mistake in that simple application.

15 replies

October 13, 2010

danilocesar danilocesar
Lab Rat
161 posts

When you close your app using CTRL+C, your app.exec() method doesn’t return. Your app is killed by an unix signal called SIGINT [en.wikipedia.org]).

 Signature 

Danilo Cesar Lemes de Paula
Software Engineer

October 13, 2010

danilocesar danilocesar
Lab Rat
161 posts

If you’re looking for last second clean-up, you should use QCoreApplication::aboutToQuit [doc.trolltech.com] signal

 Signature 

Danilo Cesar Lemes de Paula
Software Engineer

October 13, 2010

danilocesar danilocesar
Lab Rat
161 posts

Ah, I’m talking about Linux… I’m not sure how windows implements those stuffs.

 Signature 

Danilo Cesar Lemes de Paula
Software Engineer

October 13, 2010

s.frings s.frings
Lab Rat
40 posts

Thanks for yout hint. I tried it but it does not work under windows, unfortunately. I do not see my debug message from the slot that I assigned to aboutToQuit() and I still get random exit codes when I pres Ctrl-C.

Did I understand you correctly, that it is normal that app.exec() does not return, so the program is simply killed without any cleanup (except that the OS closes file handles ans some other stuff).

October 13, 2010

danilocesar danilocesar
Lab Rat
161 posts

Sorry if I couldn’t made myself clear, but aboutToQuit will not work for CTRL+C.

I’ve just tried to say that aboutToQuit is safer for cleanup’s, because I thought you’re trying to do that on destructos.

About your last comment: Afaik yes… However you can watch for unix signals [doc.qt.nokia.com]. There is an interesting thread about it on stackoverflow [stackoverflow.com].

 Signature 

Danilo Cesar Lemes de Paula
Software Engineer

October 14, 2010

lyuts lyuts
Lab Rat
169 posts

I think the thread title doesn’t correspond thread’s topic. This is confusing.

 Signature 

I’m a rebel in the S.D.G.

October 14, 2010

Deleted Member # 4a2 Deleted Member # 4a2
Ant Farmer
1481 posts

chk if this faq [developer.qt.nokia.com] helps, and if you can take a similar approach to handle CTRL+C

October 14, 2010

kalle kalle
Lab Rat
42 posts

aboutToQuit() won’t help, as was previously posted, and neither will qAddPostRoutine(). Look up the man page for the signal() system function to catch SIGKILL (which is what Ctrl-C sends).

October 14, 2010

Tobias Hunger Tobias Hunger
Mad Scientist
3130 posts

kalle: Ctrl-C does send SIGTERM, not SIGKILL.

You can not catch SIGKILL, that one will always kill the application (and is what if send via the command “kill -9 <PID>”).

October 14, 2010

danilocesar danilocesar
Lab Rat
161 posts

Tobias Hunger wrote:
kalle: Ctrl-C does send SIGTERM, not SIGKILL.

You can not catch SIGKILL, that one will always kill the application (and is what if send via the command “kill -9 <PID>”).

Afaik no.

SIGTERM is the default signal sent by kill (without -9)
SIGINT is the signal sent by terminal when you press ctrl-c.

http://en.wikipedia.org/wiki/SIGINT_ [en.wikipedia.org])

http://en.wikipedia.org/wiki/SIGTERM [en.wikipedia.org]

[edit: fixed link / chetankjain]

 Signature 

Danilo Cesar Lemes de Paula
Software Engineer

October 14, 2010

Tobias Hunger Tobias Hunger
Mad Scientist
3130 posts

danilocesar: You are right. I am getting old and start mixing up things:-(

October 14, 2010

s.frings s.frings
Lab Rat
40 posts

I am already familiar with signal catching under Linux and Im sure that this would be the solution for my question if I would only deveop for Linux. However, Im currently more interested in Windows.

Anyway, the abnormal program termination is not really a problem to me. I just wondered if the error message in QT Creator and the random exit code indicate a problem or if that is normal.

My understanding is now that it is normal to see a abnormal termination warning and a random exit code when I press Ctrl-C because the QCoreApplication.exec() method does not return, so the program does not return a defined exit code.

October 14, 2010

danilocesar danilocesar
Lab Rat
161 posts

@Tobias: What do you think about this:

QCoreApplication could automatically catch SIGTERM and SIGINT (and relatives for windows/mac/symbian/whatever) and return pre-defined values for the exec() method.

I don’t know if it’s portable, but it can be useful…

 Signature 

Danilo Cesar Lemes de Paula
Software Engineer

October 14, 2010

Tobias Hunger Tobias Hunger
Mad Scientist
3130 posts

@danilocesar: Qt applications already handle signals: The nice thing is that the operating system enforces this, so there is no code required in Qt (always the best way to implement anything;-). If the defaults do not work for you, then you can override it. So where is the problem?

What needs to happen when a signal is received is usually platform dependent, so there is little to win by proving an abstraction in Qt.

May 2, 2012

aekam aekam
Ant Farmer
81 posts

i have written a console application.
now suppose if i send SIGINT to the application, will it get terminate immediately.??
or it will complete all the pending stuff and then return.??

[EDITED: aboutToQuit signal may help, once the SIGINT is received and event loop returns, this signal will be emitted and clean up can be done in the slot connected to it… right.??]

 Signature 

If you take care of inches, you won’t have to worry about miles… :)

 
  ‹‹ Manipulating Images      trouble linking custom widgets between different libraries ››

You must log in to post a reply. Not a member yet? Register here!