QApplication stuck in constructor - c++

I have following code compiled with Visual Studio 2013, x64 target and linked with QT 5.4. It compiles fine and it generates the executable.
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QMainWindow mainWindow;
mainWindow.show();
return app.exec();
}
If I try to run it from the IDE it gets stuck on the line: QApplication app(argc, argv);, but if I run the executable directly works as expected, it creates a small windows and the process gets closed after the windows is closed. I have same behavior in both debug and release configurations.
Is anything wrong with the code? What may cause this weird problem?
EDIT: It gets stuck is something like an infinite loop inside. Unfortunately the call stack doesn't help me much about that because it looks like this if I hit "break all":
Qt5Core.dll!0000000066ae3291() Unknown
Qt5Core.dll!0000000066aa941f() Unknown
Qt5Core.dll!0000000066ad0da7() Unknown
Qt5Core.dll!0000000066b302b6() Unknown
Qt5Core.dll!0000000066b2e3a1() Unknown
Qt5Gui.dll!000007fee4fcde9f() Unknown
Qt5Widgets.dll!000000006441e1dc() Unknown
MyAwesomeApp.exe!main(int argc, char * * argv) Line 10 C++
EDIT1:
After changing the configuration to Win32, it works as expected when started from IDE.

Related

Qt program compiling with MSVC crash on app.exec()

I needed to include OpenCV into my program, so after long time, I gave up to set it up on mingw (which was fully functional for my program) and installed MSVC17 compiler and debugger. But when I run my application with MSVC, program crash with 0x0000005 code, access violation on 0xfffffff.
It always crash on app.exec() in main.cpp
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
MyClass myClass;
myClass.getView()->setMinimumWidth(900);
myClass.getView()->setMinimumHeight(600);
QQuickWindow *quickWindow = qobject_cast<QQuickWindow *>(myClass.getView());
quickWindow->setFlags(Qt::FramelessWindowHint);
//Connecting signal from QML to exit application
QObject::connect(myClass.getView()->engine(), SIGNAL(quit()), &app, SLOT(quit()));
quickWindow->show();
return app.exec(); //Here it crash
}
As I said, it fully worked with mingw (32 bit).
Can MSVC2017 x64 somehow crash when building app, that was previously used by mingw x32? Do anyone know how to fix it?
Thank you very much!
//EDIT:
Somehow, it works for release, but not for debug mode.
//EDIT:
It crashes only when app is showing visible QML object
This sounds like a 32 vs. 64-bit issue.
Try updating the dependencies on your item and see if that resolves it, you should be able to re-build the raw source to x64 if you specify that CPU build, and update any dependencies that are x86.
Or, run the app in 32-bit mode.

Using Visual Leak Detector with a QApplication

I am trying to locate the memory leaks in my Qt application. I already have used Visual Leak Detector for some other projects, but VLD writes the output to the console window.
My problem now is that when using a QApplication, no console window, and therefore no output from VLD, is shown. I am using Visual Studio 2015 with the Qt VS Tools plugin.
Is there a way to force the application to show the console window? Or maybe a way to write the output generated by VLD to a file?
How I start up my application:
#include "mainwindow.h"
#include <vld.h>
#include <QApplication>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
As ssbssa pointed out as a comment, the problem could be solved by setting ReportTo and ReportFile in vld.ini found in the installation folder of VLD:
change ReportFile = to ReportFile = memory_leak_report.txt or something like that.
change ReportTo = debugger to ReportTo = file or ReportTo = both.
Now the output produced by VLD will be written to the specified file.

Must construct a QApplication before a QWidget & Invalid parameter passed to C runtime function

I finished migrating an application from Qt4 to Qt5, it compiles and everything but it crashes at a certain point. I am debugging it and trying to find why but I have reached a dead end:
Here is the stack:
main.cpp line 373:
TouchSwibz w(NULL, NULL, renderMode ? renderMode : AppSettings::RASTERMODE);
When it reaches the breakpoint and I try to go further, it crashes with the usual
"This application has requested the Runtime to terminate it in an
unusual way."
And the aplication output shows
QWidget: Must construct a QApplication before a QWidget
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.
I have thought maybe its because the widget is being initialized when the main window is being created, but what can be done to solve this? What would be a good workaround? I dont even know if this is the real issue.
I work under Windows 7 x64 using Qt 5.2.1 and compiling with mingw 4.8 32bit, the application is in 32bits also. Everything is compiled with the same kit.
I dont know what other useful information I can provide. I tried stepping inside the QwtSlider constructor but I cant.
I managed to solve it by compiling all the libraries in debug mode, turns out having libraries in release mode while building your application in debug mode will make undefined behaviour happen.
You're most likely having non-local instances of QWidget type. By definition, those will be initialized before main starts executing, so before QApplication gets constructed. The code below reproduces the problem:
#include <QLabel>
#include <QApplication>
QLabel label("Hello, world!");
int main(int argc, char ** argv)
{
QApplication app(argc, argv);
label.show();
return app.exec();
}
The fix is to delay the construction until there is a QApplication object:
#include <QLabel>
#include <QApplication>
// Won't ever be a dangling pointer.
QPointer<QLabel> label;
int main(int argc, char ** argv)
{
QApplication app(argc, argv);
QLabel label_("Hello, world!");
label.reset(&label_);
label->show();
return app.exec();
}
I just solved a similar problem, and here is my detailed situation and solution:
I use VS with Qt add-on;
and Qt version is 5.7 64 bit (but this is not important)
I compiled successfully in both debug and release mode;
I could run it in debug mode but not in release, where caused an massage "Must construct a QApplication before a QWidget".
[IMPORTANT] I first compiled and tested it under DEBUG mode, and then I met some computational thresh-hold that encouraged me to use RELEASE mode.
[IMPORTANT] I used some third-party library, related to Qt GUI component, that requires you to add an external dependency to the project.
[IMPORTANT] I just copy the configures in the Project Property page from DEBUG to RELEASE, like exteral C++ library or External Dependencies.
I finally found the reason. In the project's Property Pages dialog box -> Linker folder -> Input property page -> Additional Dependencies, one of the external library should be replaced as a release version one, which have different name, in my case, QGLViewerd2.lib to QGLViewer2.lib.

"Using OS scope before setting MAKEFILE_GENERATOR" in Qt

I have downloaded and installed Qt and I'm learning how to use it.
So, I created a new project myfristqt (empty project). I then added a main.cpp file with this code:
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
return app.exec();
}
First thing I noticed #include <QApplication> line is hightlighted with a red line as if QApplication was an unknown identifier. So, I compiled it to see what happens and here's the error I got:
(internal):1: error : Using OS scope before setting MAKEFILE_GENERATOR
Any idea why this is happening ? I'm using Windows XP
MAKEFILE_GENERATOR is a qmake variable.
This variable contains the name of the Makefile generator to use when generating a Makefile. The value of this variable is typically handled internally by qmake and rarely needs to be modified.
It define in QTDIR/mkspecs/PLATFORM/qmake.conf. Where PLATFORM is maybe cygwin-g++, win32-msvc200x on your Windows XP.

Qt4.8.1 undhandled exception at QMdiArea::addSubwindow

I compiled Qt 4.8.1 with these instructions:
http://www.holoborodko.com/pavel/2011/02/01/how-to-compile-qt-4-7-with-visual-studio-2010/
for my system (Visual Studio 2010, x64). The compilation worked and everything seemed Ok.
While compiling there is a lot of output, but it's very fast and therefore I can't read it so i suppose that shouldn't be a problem.
After I compiled succesfully my current project I got an unhandled runtime exception. After a while I discovered that it comes from the QMdiArea::addSubwindow function, which seems to throw the exception (I'm not able to catch it with overriden notify function though). So I searched for an example project to see if it's my code or something else. I found this example here:
http://www.codeprogress.com/cpp/libraries/qt/qMdiAreaAddSubWindow.php
And it works fine in 32bit mode as well as debug mode of x64, but again at the
QMdiArea::addSubwindow function there is an unhandled exception. Has anyone an idea what's wrong or had the same problem?
Currently I'm recompiling Qt to get the debug information again (i cleaned it beforehand).
But maybe someone had the same problem and a solution for me.
//Update:
The code is here (the exact same as in the link)
#include <QApplication>
#include <QMainWindow>
#include <QMdiArea>
#include <QMdiSubWindow>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QMainWindow window;
window.setFixedSize(800,600);
window.setWindowTitle(QString::fromUtf8("My QMdiWindow"));
QMdiArea* area = new QMdiArea();
area->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
//Create QMdiSubWindow
QMdiSubWindow* subWindow = new QMdiSubWindow();
subWindow->setFixedSize(200,100);
//Add subWindow to Main QMdiWindow here
area->addSubWindow(subWindow);
window.setCentralWidget(area);
window.show();
return app.exec();
}
//Update2:
I opened another discussion here.
Ok, I found the problem. It is a bug in the MSVC++ compiler. Installing the service pack and recompiling Qt again helps.