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

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.

Related

QApplication stuck in constructor

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.

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.

QML C++ application crash outside Qt Creator

I am using Qt 5.1.1 with Visual Studio 2012 compiler. I have a Quick 2 application with a c++ backend. When i'm running application in Qt Creator everything looks good.
But when i'm trying to launch application outside from Qt Creator it crashes.
I have all Qt*.dll 's in my folder, also have D3DCompiler_46.dll.
I tried to access some filed in my QML file from C++ part, and I see that after crash in Visual Studio debugger that the crash look like null pointer exception.
QObject *openButton = instanceRoot->findChild<QObject*>("openFileButton");
openButton->setProperty("enabled",QVariant(enabled));
I have all my resources declared in qrc file, and it also included in my .pro file as
RESOURCES += res.qrc
By this article that menas, that all my qml files should be emdeded in my exe files. But I think it shouldn't.
How can I find and fix this problem?
UPD:
Here is my main function:
int main(int argc, char *argv[]) {
QGuiApplication a(argc, argv);
Q_INIT_RESOURCE(res);
QQuickView view;
Decrypter dec;
view.rootContext()->setContextProperty("Decrypter", &dec);
QObject::connect(view.engine(), SIGNAL(quit()), &a, SLOT(quit()));
view.setSource(QUrl("qrc:/PlayerGUI.qml"));
view.show();
QQuickItem *rootObject = view.rootObject();
Player *player = new Player(rootObject);
return a.exec();
}
UPD2: Two persons from comments were confused by one string of the code:
view.engine()->addImportPath("../QtAV/qml/");
So, I deleted it, and still have a crash(

"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.