PDA

View Full Version : can't exit application without various qt errors



dit
21st May 2018, 21:31
I have QtCoreApplication running from main.
the application runs beautifully, at some point I want to invoke shutdown of the application (and process). I tried to use delete later methods but I still get
QObject was deleted directly. core dumped

I'm really looking for a quick an dirty solution. I don't care about shutdown sequence, or in what goes in the other threads I'm running. I only want to avoid getting this log error. but the following things I tried got me no where.

a. when I try to use(from the same thread as the main thread):
QCoreApplication::instance().quit()
or
QTimer::singleShot(0,QCoreApplication::instance(), SLOT(quit()))
QEventLoop: can't be used without QApplication
b. If I try exit(0); I get
"QThread: Destroyed while thread still running"

what is the minimum requirement qt needs to have as to avoid any possible error at shut down? (and where I can read about it in qt documentation, all I could google up is people facing those kind of mess and do X do Y .. )
thanks in advance!!

ChrisW67
23rd May 2018, 11:51
So you main looks like this?


int main(int argc, char**argv)
{
QCoreApplication app;
YourApplicationClass myAppObject; // something like a QMainWindow would be to a GUI app
return app.exec();
}

and your code is sitting in the exec() loop then you should be able to call:


QCoreApplication::exit(0); // it is static

from anywhere in the main thread, or use a signal connected to QCoreApplication::quit().

If you are trying to terminate the application before line 5 is reached, then there is no event loop running. That means timers and queued signal/slot connections will not function: possibly the cause of the message in your option (a). If you cannot redesign your main() along usual Qt lines then you exit from this scenario by allowing execution to fall through to the exec() call with a flag set. If flag set then you never call exec().

Your option (b) is using the library function to summarily terminate the program. Its generally poor form to not clean up your threads, which is why the warning is issued by Qt.

dit
23rd May 2018, 19:53
hi Chris,
I really appreciate you answering.. I really want to have a solution and can't seem to progress.
so yes my main looks like:
int main(int argc, char**argv)
{
QCoreApplication app;
return app.exec();
}
and since the application is up and doing a lot of stuff using signal slot- so the eventLoop is running.
so there must be some other reason of me getting
"QEventLoop: can't be used without QApplication"
what can be the reason for that? where can I read all about that?
how can it be that the application has ended but its eventLoop still running? Is there any way to debug what is going on there?

so should "QCoreApplication::instance().quit()" be enough to exit the process with no errors?

thanks again

d_stranz
23rd May 2018, 23:30
If that is all that is in your main(), then your app does nothing. Obviously you are doing something else that you aren't showing us, like trying to create global QObject or QThread instances. Since global variables are initialized -before- main() is executed, you have no event loop running, not even a qApp() instance yet. If you try to do things with global variables that require an event loop or interacting with the qApp() instance, then that's the most obvious reason for your error message.