QApplication crashes on Constructor
-
Hi,
I have a strange problem. In my Application I have to create a QApplication. It's not in the main method so I have to simulate the argc and argv parameters. On 2 tested PCs (first Suse 10.2 with QT 4.4.1, the second Suse 11.3 with QT 4.6.3) everything works fine. On the third (Suse 11.2 with QT 4.5.3) it crashes in the Constructor of QApplication. Does anybody know this problem?Thx for the help
KevinCodefragment:
@ static char* argv[] = {"debugger"};
static int argc = 1;
app = new QApplication(argc,argv);
QApplication::setStyle(new QCleanlooksStyle);@Errormessage from the third PC:
@Program received signal SIGSEGV, Segmentation fault.
0x00007ffff219271b in memcpy () from /lib64/libc.so.6
(gdb) backtrace
#0 0x00007ffff219271b in memcpy () from /lib64/libc.so.6
#1 0x00007fffe9ecae0e in QString::realloc(int) () from /usr/lib64/libQtCore.so.4
#2 0x00007fffe9ecb37a in QString::append(QLatin1String const&) () from /usr/lib64/libQtCore.so.4
#3 0x00007fffe9f217ad in ?? () from /usr/lib64/libQtCore.so.4
#4 0x00007fffe9f21986 in ?? () from /usr/lib64/libQtCore.so.4
#5 0x00007fffe9f21c41 in ?? () from /usr/lib64/libQtCore.so.4
#6 0x00007fffe9f2200c in QResource::isCompressed() const () from /usr/lib64/libQtCore.so.4
#7 0x00007fffe9f221cf in ?? () from /usr/lib64/libQtCore.so.4
#8 0x00007fffe9f2230d in ?? () from /usr/lib64/libQtCore.so.4
#9 0x00007fffe9ef6242 in QAbstractFileEngine::create(QString const&) () from /usr/lib64/libQtCore.so.4
#10 0x00007fffe9f092fd in ?? () from /usr/lib64/libQtCore.so.4
#11 0x00007fffe9f03ae9 in QFile::exists(QString const&) () from /usr/lib64/libQtCore.so.4
#12 0x00007fffe9e81b60 in ?? () from /usr/lib64/libQtCore.so.4
#13 0x00007fffe9e81d19 in ?? () from /usr/lib64/libQtCore.so.4
#14 0x00007fffe9e81d6e in ?? () from /usr/lib64/libQtCore.so.4
#15 0x00007fffe9e81e7c in QLibraryInfo::location(QLibraryInfo::LibraryLocation) () from /usr/lib64/libQtCore.so.4
#16 0x00007fffe9f70c3b in QCoreApplication::libraryPaths() () from /usr/lib64/libQtCore.so.4
#17 0x00007fffe9f712b8 in QCoreApplication::init() () from /usr/lib64/libQtCore.so.4
#18 0x00007fffe9f71375 in QCoreApplication::QCoreApplication(QCoreApplicationPrivate&) () from /usr/lib64/libQtCore.so.4
#19 0x00007fffe943e764 in QApplication::QApplication(int&, char**, int) () from /usr/lib64/libQtGui.so.4@ -
@Lukas that's the case.
On my PC the window got the title right and it works without problems only on the PC of my colleague.
@Volker I can't. But I can prvide a simple program with a similar error. On some PCs it works on others it doesn't. That's why I'm so confused.
@#include <iostream>
#include <QApplication>
#include <QStringList>
#include <QLabel>int main( int argc, char *argv[] )
{
std::cerr << "before QApplication\n";static char* args[] = {"AppName"}; static int argsCount = 1; QApplication app( argsCount, args ); //QApplication app( argc, argv ); QStringList::iterator it; QLabel label("MyLabel"); label.show(); for ( it = app.arguments().begin(); it != app.arguments().end(); ++it ) { std::cerr << (*it).toStdString() << "\n";
// std::cerr << (*it).toAscii().constData() << "\n";
// std::cerr << (*it).toLocal8Bit().constData() << "\n";
}app.exec(); std::cerr << "after QApplication\n"; return 0;
}
@Kevin
[EDIT: fixed @-confusion - the forum software messes that up, sorry; Volker]
-
Hi,
your problem is located here:
@
for ( it = app.arguments().begin(); it != app.arguments().end(); ++it )
{
std::cerr << (*it).toStdString() << "\n";
}
@arguments returns a copy of the array. you have an iterator in the temporary copy of the array and access it later on. On the other side, it will never be end, as that is a second copy of the array...
If you do it that way, it works:
@
QStringList argsList = app.arguments();
for ( it = argsList.begin(); it != argsList.end(); ++it )
{
std::cerr << (*it).toStdString() << "\n";
}
@ -
I could reproduce the crash now, but it's most likely not from the non-standard QApplication arguments, but from a wrong usage of the arguments. That method returns a new QStringList object on every call, so you have two different QStringLists in your for loop, returning different iterators. So the loop is probably entered too often and the iterator is pointing to a non-existing entry.
You can circumvent this by obtaining a single QStringList instance and iterate over that one (which is recommended in the docs for performance reasons anyways):
@
QStringList::iterator it;
QStringList argList = app.arguments();for ( it = argList.begin(); it != argList.end(); ++it )
{
std::cerr << (*it).toStdString() << "\n";
}
@ -
Thx for this fix. Unforunately it doesn't solve my original problem. I'm writing a Plugin for a much larger programm and this doesn't have -QT- Qt in it only my Plugin has. So I can't call the variables from the commandshell (original argc and argv). A workaround which works on my PC is to create two static variables and parse them to the constructor of the QApplication. Every few ms my Plugin is called and checks all events because I can't called app.exec() because it's not in the mainthread.
@Debugger::Debugger()
: BaseClass( )
{
static char* argv[] = {"debugger"}; //Title for the widget
static int argc = 1; // 1 argument in argv
app = new QApplication(argc,argv); //here is the crash on some PCs all others work fine
QApplication::setStyle(new QCleanlooksStyle);
// Initialize viewer
main = new MainDebuggerWindow();
main->show();app->processEvents(); // Call it one time, so that the widget appears
}void Debugger:update()
{
app->processEvents(); // Process the next x11 event
}
@ -
[quote author="KevinV" date="1315468085"] Every few ms my Plugin is called and checks all events because I can't called app.exec() because it's not in the mainthread.
@[/quote]Ahaaaa :-)
QApplication initializes some GUI stuff. This must be done in the main thread and you must not create GUI elements from your secondary thread.