PDA

View Full Version : Qt-based dll: qApp = 0 !!



Elder Orb
13th September 2006, 14:50
I've created Qt-based gui dll and it works good if is used with shared version of Qt. But when I try to use static Qt, qApp in dll becames 0 !.. Dll creates QApplication only if application doesn't have its own (I do it in special fuction init()). But in the case of static Qt qApp inside dll is always 0, even of app owns QApplication instance!.. What is it? strange bug or bad feature ?

wysota
13th September 2006, 16:03
I guess it might be a normal behaviour with static libraries. Maybe you could still use QCoreApplication::instance()?

Elder Orb
13th September 2006, 21:34
I guess it might be a normal behaviour with static libraries.
But why? I just can't understand... ;(


Maybe you could still use QCoreApplication::instance()?
afaik qApp is just a macro for QCoreApplication::instance(). All I probably need is to give to the dll a pointer to the QApplication and somehow make QApplication::instance() inside dll to return a pointer to application's QApplication.. I've tried to undef and define qApp to return that pointer but hasn't succeed :(.. Maybe there is a way to use 2 instance of QApplication - one in application, other in dll ?

wysota
13th September 2006, 23:05
No. The problem is probably due to the fact that the library is STATIC. It doesn't have access to QCoreApplication::instance() present in the main application, because the latter uses a relocated library (meaning it resolves the symbols during dynamic link time) and the static library's QCoreApplication::instance() points to the method (and all pointers it uses) which is present inside the library code (as it is statically linked -- it only knows a permanent address of the method). It has no way of reaching any code (be it a class, a method or a global pointer) from the dynamic code -- there are two "QCoreApplication::instance()" methods in the application and each of them returns a different pointer. There is no way of getting around this but to use some relocable (dynamic) code in the "plugin" to "reach" into proper QCoreApplication::instance() -- the dynamic code has to be linked against the dynamic QtCore library or should at least be able to dlopen() it and return a pointer to its QCoreApplication::instance() method which you could then call. Due to the fact that it would reach into the same dynamic library, the pointer would be the same and you would get a correct value, pointing to the right QCoreApplication instance. At least in theory. :eek:

Elder Orb
14th September 2006, 07:54
No. The problem is probably due to the fact that the library is STATIC. It doesn't have access to QCoreApplication::instance() present in the main application, because the latter uses a relocated library (meaning it resolves the symbols during dynamic link time) and the static library's QCoreApplication::instance() points to the method (and all pointers it uses) which is present inside the library code (as it is statically linked -- it only knows a permanent address of the method). It has no way of reaching any code (be it a class, a method or a global pointer) from the dynamic code -- there are two "QCoreApplication::instance()" methods in the application and each of them returns a different pointer. There is no way of getting around this but to use some relocable (dynamic) code in the "plugin" to "reach" into proper QCoreApplication::instance() -- the dynamic code has to be linked against the dynamic QtCore library or should at least be able to dlopen() it and return a pointer to its QCoreApplication::instance() method which you could then call. Due to the fact that it would reach into the same dynamic library, the pointer would be the same and you would get a correct value, pointing to the right QCoreApplication instance. At least in theory. :eek:

Thank you for explanation! Now I see that not all is so easy as it looks :).. I'll try above approach if I understood it right and if my last crazy idea hasn't succeed. (I've thought about making custom QApplication which instance() method will return pointer which I pass from main app to dll)

mif
14th September 2010, 15:57
Hello,

I know that this Thread is quite old, but I have the same problem at the moment.
Does anyone have a solution for that issue?

wysota
14th September 2010, 16:15
The solution is to not use static linking.