QThread: "Destroyed while thread is still running" in a single threaded program - c++

int main(int argc, char** argv)
{
QApplicaiton app(argc, argv);
// parsing other arguments of argc,argv
return app.exec();
}
My problem is the following:
Function may be returned during parsing other arguments (without reaching app.exec()) and when QApplication object is deleted I am getting following error message QThread: Destroyed while thread is still running. As a possible solution I am trying to create QApplication after argument parsing is done.
I have tried app.thread()->quit(); before return statement but it doesn't help.
When QApplication object is created it removes specific arguments (-style, etc.) from argc, argv.
Is it possible to get them from argc, argv manually without creating QApplication object?
It is weird that in a single threaded program I am getting QThread: Destroyed while thread is still running error.
The best "solution" I have found so far is to create QApplication dynamically and not to delete it. The memory leak is not an issue because it is leaked just before program exits.

Related

After initializing class member app crashes with SIGSEGV [duplicate]

This question already has an answer here:
C++ - segmentation fault when inheritance
(1 answer)
Closed 6 years ago.
I have Qt application with Application class:
class Application : public QApplication {
public:
Application(int argc, char** argv);
~Application();
};
everything works fine until I add
private:
const QString NAME = "QtImageViewer";
after that app crashes 9 of 10 times when I try to run it
main looks like
int main(int argc, char** argv)
{
Application app(argc, argv);
app.setApplicationName("QtImageViewer");
MainWindow mainWindow;
mainWindow.show();
return (app.exec());
}
I'd like to know what's wrong because I have no idea.
Edit:
GDB backtrace
Valgrind
Fixed it, same problem as here.
My Application class constructor signature is Application(int argc, char** argv); but QApplication has QApplication(int &argc, char **argv)
Changed Application(int argc, char** argv); to Application(int& argc, char** argv); and everything works.
Thanks to all for help.
I'd comment, but I don't have enough reputation at the time of this writing. For the record, I cloned your repository you linked to. I notice you don't seem to be using Qt Creator but are probably just using cmake. My Qt 5 installation isn't where yours is, so I just threw everything into Qt Creator and built and ran the project from there.
The program runs and closes just fine, even with the change you suggest breaks it for you. (Built and run on Ubuntu 16 with Qt 5.5, g++ 5.4.0.)
Additionally, I may be misunderstanding Igor's comment, but since Application is made on the stack, no additional heap deallocation is introduced to ~Application().

QTcpSocket not emit any error or connected signals

I have a small project which send some data over network using QTcpSocket. The server works fine but the client(code here) seems does nothing. If I set breakpoint at tcpSocket.connectToHost("127.0.0.1",port); it does jump in, but not any slots I defined.
I can't figure out what's wrong. I think the environment is ok because I can build 2 working examples from Qt GUI Programming
Any ideas are appreciated.
You do not have a QApplication instance and thus no event loop which does all the event / signal&slot handling.
So you at least need a QCoreApplication instance like this in main.cpp:
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Client client;
client.connectToServer();
return a.exec();
}

Program crash after setting QApplication::style twice

#include <QApplication>
int main() {
QApplication::setStyle("windows");
QApplication::setStyle("windows");
}
This program produces Segmentation fault (core dumped). My qmake version is 4.7.2. Is this a Qt bug or my version is too old?
You must create an instance of QApplication before you set it's style. From the documentation
Ownership of the style object is transferred to QApplication, so
QApplication will delete the style object on application exit or when
a new style is set and the old style is still the parent of the
application object.
I'm assuming it's crashing because there is no QApplication to take ownership of the style. In general, creating the QApplication is one of the first things you should do.
#include <QApplication>
int main() {
QApplication a(argc, argv);
QApplication::setStyle("windows");
QApplication::setStyle("windows");
}

How to Run a whole QApplication Asynchronously?

I'm trying to run a modified qt-programm as a library. I need it not not block the main execution.
So what I want to do is to run the QApplication and commence the execution of the main application. How do I achive this?
My first thought was to run it in a seperate thread.
void MyClass::execute() {
someClass = someClass::instance();
std::thread t1(&MyClass::startApp, this);
someClass->someFunction();
someClass->doMoreStuff();
}
void MyClass::startApp() {
QApplication app(argc, argv);
app.exec();
QCoreApplication::quit();
}
but this results in an Call to '__invoke' is ambiguous error. Though I don't know why/where __invoke is overwritten and how to handle this error. :(
So how can I accomplish that the QApplication doesn't block the main execution?
Design pattern is totally wrong, QApplication as well as QCoreApplication are not supposed to be multiplied inside one application. What you should do for example is make your own class like:
class LibraryCore: public QObject {
}
and substitute QApplication to this class inside your future library sources. Then you should implement needed methods in LibraryCore (ones which are used inside application) making them work properly..

What is "QApplication app(argc, argv)" trying to do?

#include <QtGui/QApplication>
#include <QtDeclarative>
#include "qmlapplicationviewer.h"
int main(int argc, char **argv) {
QApplication app(argc, argv);
QmlApplicationViewer viewer;
viewer.setMainQmlFile("app/native/assets/main.qml");
viewer.showFullScreen();
return app.exec();
}
My C++ is a bit rusty. Can someone please explain to me what is "QApplication app(argc, argv)" trying to do ?
Is it trying to declare a function which takes in 2 arguments (argc and argv) and return a variable of type QApplication ?
The line
QApplication app(argc, argv);
creates a new instance of type QApplication and invokes the constructor of this class. In your example, the variable app now stores this instance. It is somewhat (semantically) a shorthand of this:
QApplication app = QApplication(argc, argv);
Here's a quote from Qt Docs:
The QApplication class manages the GUI application's control flow and
main settings.
QApplication contains the main event loop, where all events from the
window system and other sources are processed and dispatched. It also
handles the application's initialization, finalization, and provides
session management. In addition, QApplication handles most of the
system-wide and application-wide settings.
For any GUI application using Qt, there is precisely one QApplication
object, no matter whether the application has 0, 1, 2 or more windows
at any given time. For non-GUI Qt applications, use QCoreApplication
instead, as it does not depend on the QtGui library.
The QApplication object is accessible through the instance() function
that returns a pointer equivalent to the global qApp pointer.
So, the line
QApplication app(argc, argv);
creates an instance of the QApplication class.
QApplication is a Qt class that contains the main event loop.
When you write QApplication app(argc, argv);
you are creating a object app of this class, by calling its constructor with argc and argv
When int main(int argc, char **argv) is called while running the program, int argc is intialized to contain the number of arguments passed while running the program. char **argv contains an array of arguments passed to the program when executing it.
char* argv[0] will contain (point to) the name of the program, while the subsequent elements will point to the other arguments passed.
argc and argv are in turn, passed to the constructor of QApplication, so that one can pass Qt specific arguments when running your program.
For an example of such arguments try running ./yourProgramName --help in a terminal window
app() is not a function, it is a constructor call.
If you come from C# or Java or something, imagine it as
QApplication app = new QApplication( argc, argv );
Just that app would be a pointer this way, while it actually is the object itself if it is created like in your example.
In short, Qt needs a QApplication instance to run so signals&slots are processed (if you are using them) and events like painting etc are handled