PDA

View Full Version : qApp null if QCoreApplication defined in shared library



magland
29th October 2011, 14:08
If I do:



int main(int argc, char *argv[])
{
QCoreApplication app(argc,argv);
qDebug() << "qApp=" << qApp;
....
}


The of course qApp is not null. But if define MyCoreApplication in a shared library (dll on windows), and I do




int main(int argc, char *argv[])
{
MyCoreApplication app(argc,argv);
qDebug() << "qApp=" << qApp;
....
}


Then qApp is NULL! That is not what I would expect. The problem is that I have qApp->applicationDirPath() calls all over the place, and (qApp->setProperty()), so I can't affort to not have access to qApp in the main app and in the shared lib.

If I do this hack...



int main(int argc, char *argv[])
{
MyCoreApplication app(argc,argv);
QCoreApplication app2(argc,argv);
qDebug() << "qApp=" << qApp;
....
}


Then it works... however, I suspect I now have two versions of qApp, which is okay for qApp->applicationDirPath(), but not good for setProperty() on qApp.

So, is this the expected behavior (even for a shared (non-static) link to a dll)? If so, then what's a good work around.

Thanks!

magland
29th October 2011, 20:29
Well I suppose the answer is that QCoreApplication::instance() is static, and is therefore not shared between shared libraries. However, it still raises the question of how to best share the qApp with shared libraries. One can pass it as a parameter, but it gets messy. I have a workaround for my particular application, but I wonder if there is a concensus way to handle this.

magland
30th October 2011, 01:01
I don't think what I am seeing is the expected behavior.... shouldn't the qApp automatically point to the same instance of QCoreApplication whether I am in the main program or a shared library? I found several posts to indicate that this should be the case. But I'm the only one commenting on this thread... would appreciate some help, as I am STUCK!

Shouldn't I be able to define "QCoreApplication app;" in the main.cpp and then access that via qApp in a shared library? (Kind of the opposite problem as outlined above) Well that doesn't work either!

Santosh Reddy
30th October 2011, 02:50
Why do you want to create an instance on QCoreApplication in a shared library?

I believe all you need would be few static calls, which can be always used without instance.

I suggest to not to use
qApp->applicationDirPath(), instead use
QCoreApplication::applicationDirPath() further you can get the instance using
QCoreApplication app = QCoreApplication::instance(); then you can use
app->setProperty(..);

magland
30th October 2011, 14:04
Thanks Santosh. But this is precisely my problem... that static functions (including QCoreApplication::applicationDirPath(), QCoreApplication::arguments(), QCoreApplication::processEvents()) do not seem to work in a shared a library, if the QCoreApplication was instantiated in the main().

I really think this is a fundamental issue, and there most be a work around.. but I haven't been able to find it.

magland
31st October 2011, 19:39
Turns out that the shared library was created in debug mode, and the main executable was in release. This was apparently the SILENT cause of many terrible problems with static variables.

If somebody would have just said something like: "No, magland, that is not the expected behavior, qApp should work perfectly in shared libraries", then it would have saved me a lot of time. Anyway, that's the way it goes.

If somebody comes across this thread... be sure to specify CONFIG += release everywhere!