Page 1 of 3 123 LastLast
Results 1 to 20 of 43

Thread: QWidget: Must construct a QApplication before a QPaintDevice crash error

  1. #1
    Join Date
    Aug 2009
    Posts
    81
    Platforms
    MacOS X Windows

    Default QWidget: Must construct a QApplication before a QPaintDevice crash error

    i am having 2 different plug-ins that work inside a non QT application... as long as i stay in debug mode all fine... when i go to release and deploy them... as long as i use the same plug in all fine...
    when i start switching from one of the plug-in to the other i start to get this QWidget: Must construct a QApplication before QPaintDevice ... i am sure my QApplication has been created before i checked it out

    it sounds more like the frameworks which are exactly the same (but dynamic, deployed and independent for each plug-in) are not unloaded and create a conflict

    I taht possible? how can you make sure when you quit one plug-in that use QT frameworks without quitting the application that has launched the plug-in that there cannot be conflict of frameworks with a subsequent plug-in that is launched in the same session

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QWidget: Must construct a QApplication before a QPaintDevice crash error

    I have no idea what, "I taht possible?" is referring to or, indeed, much of what your post is trying to say.

    What do you mean by "plug-ins that work inside a non QT application"? Are you talking about ActiveX controls on Windows or something else?
    What do you mean by , "switching from one of the plug-in to the other"? Do you mean moving focus between widgets presented by the "plug-ins", loading and unloading the "plug-ins", or something else?

    Where and how is the (singular) QApplication instance created? Where and how is that QApplication instance destroyed?

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QWidget: Must construct a QApplication before a QPaintDevice crash error

    I think what he is trying to say is, that he has a non Qt application, which uses plugins which link to Qt.
    This will also account to the sort of the problem he (probably) refers to regarding the construction of a QApplication.

    @OP
    Do you need to use Qt GUI facilities or services that need an event loop?
    Consider very good what you relay need in your Qt plugins, it might be that you can get rid of all the event loop dependent code, which will free you from the need to have an instantiated QApplication/QCoreApplication in your plugins.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  4. #4
    Join Date
    Aug 2009
    Posts
    81
    Platforms
    MacOS X Windows

    Default Re: QWidget: Must construct a QApplication before a QPaintDevice crash error

    Hello again, thank you,

    yes i have non Qt application (not made by me so i have no control over it. This application can call plugins following a user interaction. So a user can decide to launch a plug-in... Those plug-in use QT and do have a user interface and multiple QT frameworks are needed ( i do not use static QT frameworks)

    i have sub classed QApplication

    Qt Code:
    1. class myQApplication : public QApplication
    2. {
    3. public:
    4. myQApplication(int &argc, char **argv);
    5.  
    6. protected:
    7. bool event(QEvent *);
    8.  
    9. };
    To copy to clipboard, switch view to plain text mode 

    and as soon as the plug-in starts before any event loop i call
    my_qapp = new myQApplication(argc, argv);


    then my event loop

    when i quit the plug-in i do
    delete my_qapp;


    All this works perfectly well in debug mode..

    when i go to release i need to deploy it so each plug-in has its own copy of the frameworks.

    In release mode, as long as i use one plug-in, ii can do multiple launch and quit, all works fine. The problem occurs when i quit one of the plug-in and launch the second one... i got this error message
    and in console i can see some message that says that the framework of plug-in number 1 or the framework or plug-in number 2 might be used and that is where i am getting really confused.

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QWidget: Must construct a QApplication before a QPaintDevice crash error

    There are some things you are not saying - for example, where/when (if at all?) you start the the QApplication event loop? (exec())


    Try calling quit() on your QApplication before you delete it in your DLL, see if this helps.
    Last edited by high_flyer; 12th July 2012 at 10:12. Reason: Removed inacurate statements.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Aug 2009
    Posts
    81
    Platforms
    MacOS X Windows

    Default Re: QWidget: Must construct a QApplication before a QPaintDevice crash error

    the application uses only one plug-in at a time..


    there are not many steps.. the crash occurs during the initialisation of my QMainWindow
    as i am giving a minimum and a maximum size (setMinimumSize...)

    so i have not reached the even loop yet

    Qt Code:
    1. {
    2. MainWindow *w;
    3.  
    4. my_qapp = new myQApplication(argc, argv);
    5.  
    6. // MainWindow is a sub class of QMainWindow
    7. w = new MainWindow();
    8.  
    9. my_qapp->exec();
    10.  
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 


    i do have any quit() when i delete my QApplication at exit time i will try that

  7. #7
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QWidget: Must construct a QApplication before a QPaintDevice crash error

    Quote Originally Posted by eric_vi View Post
    Qt Code:
    1. {
    2. MainWindow *w;
    3.  
    4. my_qapp = new myQApplication(argc, argv);
    5.  
    6. // MainWindow is a sub class of QMainWindow
    7. w = new MainWindow();
    8.  
    9. my_qapp->exec();
    10. }
    To copy to clipboard, switch view to plain text mode 
    what is this I dont even...

    correct:
    Qt Code:
    1. int main(int argc, char** argv) // please include method name etc so we have some context!!
    2. {
    3. myQApplication my_qapp(argc, argv);
    4.  
    5. // MainWindow is a sub class of QMainWindow
    6. MainWindow w;
    7. w.show()
    8.  
    9. my_qapp.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 
    This is not java, dear.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  8. #8
    Join Date
    Aug 2009
    Posts
    81
    Platforms
    MacOS X Windows

    Default Re: QWidget: Must construct a QApplication before a QPaintDevice crash error

    i don't know about java... i had it this way before but because of this bug i was not exactly sure where the objects would be created so i tried with a 'new' allocator to see if it was making any difference

  9. #9
    Join Date
    Aug 2009
    Posts
    81
    Platforms
    MacOS X Windows

    Default Re: QWidget: Must construct a QApplication before a QPaintDevice crash error

    i tried to put a quit() when deleting the QApplication... this does not make any change...

    when within the same session i quit one plug-in to launch another (each of those plug-in are deployed and have their own set of frameworks) I get this QWidget: Must construct a QApplication before a QPaintDevice crash error... i am running on Mac OS and if i examine the console log i can see that during the launch of plugin 2 i get multiple warnings such as

    objc[722]: Class QNSStatusItem is implemented in both /Library/Application Support/plugin1/Frameworks/QtGui.framework/Versions/4/QtGui and /Library/Application Support/plugin2/Frameworks/QtGui.framework/Versions/4/QtGui. One of the two will be used. Which one is undefined.


    i really have no clue why the frameworks from the previous plug-in launch can still be used!

  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QWidget: Must construct a QApplication before a QPaintDevice crash error

    Without seeing the real code its hard to tell.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  11. #11
    Join Date
    Aug 2009
    Posts
    81
    Platforms
    MacOS X Windows

    Default Re: QWidget: Must construct a QApplication before a QPaintDevice crash error

    i have described what the code is doing there is not much more to it

    but let me describe again my issue, hopefully someone can give me some hints, because i am totally stuck...


    i am in a deployed configuration (in debug mode i have no problem at all, since i am always using a unique framework).

    i have an application (non QT), that launch thru user interaction PlugIn1 (QTmade that has been deployed and point to his own set of QT frameworks), all is fine so far...
    the user quits PlugIn1 ( doing so the QApplication from PlugIn1 is deleted and i added a quit()), the Plugin1 process is gone and the user returns to the main application then launch plugIn2 (which is similar to PlugIn 1, deployed with its own set of QT frameworks)...
    When launching PlugIn2... i can see in console mode a warning that QT might still use the frameworks of PlugIn1 (!!!!), and of course if it does so i got the QApplication before a QPaintDevice error, since PlugIn1 is no longer there and has been quitted before.

    so my question... how can i make sure the qt framework binaries are not going to be used anymore when i quit a plug-in working on QT frameworks that is used within an application, because so far it does not seem to happen for me.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QWidget: Must construct a QApplication before a QPaintDevice crash error

    how can i make sure the qt framework binaries are not going to be used anymore when i quit a plug-in working on QT frameworks that is used within an application
    The only way I know is to statically link Qt to your plugin. Otherwise always the first loaded instance of the framework will be used because libraries (as far as I know) once loaded into the process, stay there until the process quits. Therefore when plugin2 needs to resolve its Qt symbols, it does so against the set of libraries loaded for plugin1.

    However I doubt this would give you the warning you receive. I'd say you were not freeing all the resources from plugin1 properly. I would also not delete the application object instance, I don't see the point of doing so -- you can reuse the same instance that the previous plugin used.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Aug 2009
    Posts
    81
    Platforms
    MacOS X Windows

    Default Re: QWidget: Must construct a QApplication before a QPaintDevice crash error

    i might not been freeing them correctly i agree that is highly possible...

    how can i for example free the whole QTCore, QTGui etc..

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QWidget: Must construct a QApplication before a QPaintDevice crash error

    Quote Originally Posted by eric_vi View Post
    how can i for example free the whole QTCore, QTGui etc..
    I'm not talking about libraries but rather objects. Without seeing what you really do it is hard to suggest anything. For instance you need to make sure you delete all the widgets you use. This is bookkeeping that you have to do on your own. Other than that I think the whole "plugin" concept you have is incorrect but again without knowing the big picture, I'm not able to say more.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. #15
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QWidget: Must construct a QApplication before a QPaintDevice crash error

    Otherwise always the first loaded instance of the framework will be used because libraries (as far as I know) once loaded into the process, stay there until the process quits.
    Unless you unload them.

    Therefore when plugin2 needs to resolve its Qt symbols, it does so against the set of libraries loaded for plugin1.
    Are you sure about that witek?
    To my knowledge, each DLL is a process of its own - as I wrote in my previous post - each DLL is equivalent to an exe, AFAIK.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  16. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QWidget: Must construct a QApplication before a QPaintDevice crash error

    Quote Originally Posted by high_flyer View Post
    Unless you unload them.
    I don't know any way of unloading a library that was loaded by the linker. You don't even have any "handle" to the library so I don't know how you'd like to unload "library X".

    Are you sure about that witek?
    To my knowledge, each DLL is a process of its own - as I wrote in my previous post - each DLL is equivalent to an exe, AFAIK.
    When a DLL is loaded, it occupies some memory. Then this memory is being attached to every process that requests this library to be present. Therefore each DLL is loaded into memory only once and can be attached to multiple processes. There is no flow in the DLL itself, it is just a blob of code so it is not like a process -- it doesn't have its own stack, nor heap nor set of registers (and thus no instruction pointer). If you try to load the same library again, it is not loaded because it is already there. If one loads a library called "QtCore" then if someone (e.g. another library) requests a library called "QtCore" to be loaded again, the one that's already there will be used.

    Now another thing is whether requesting library X loads it into memory or uses one that looks like X that is already present in the memory. This decision has to be made when the required symbol is not already loaded into the process. If we assume that libraries are not automatically unloaded when nobody wants to use it anymore (and I think that's the case) then if you request a symbol that is in the library (like "QApplication") then it is being resolved from the library that is already loaded.

    So the bottom line is that unless MacOSX does something really weird with regard to frameworks then symbols from plugin2 are being resolved against Qt libs loaded by plugin1. I don't know what happens when the application explicitly unloads plugin1 (we don't know if that happens in this particular case), maybe it would unload Qt libs as well as I'd expect that the system holds a reference counter for each attached library on behalf of each process which at least seems to be happening on Linux -- as far as dlopen() manual is concerned. But I'm assuming plugin1 is simply not unloaded at all. In other words we don't know if an equivalent of dlclose() is called on the handle of the plugin1 library and whether it works.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. #17
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QWidget: Must construct a QApplication before a QPaintDevice crash error

    I don't know any way of unloading a library that was loaded by the linker. You don't even have any "handle" to the library so I don't know how you'd like to unload "library X".
    Under windows at least you do get a handle to your DLL.
    A reference count is done for the used instances, and when you call FreeLibrary() if the reference is 0, the DLL is removed from your apps memory.
    In such a scenario, a reference to a QApplication object would be lost in the calling application as well. (I'd expect).
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  18. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QWidget: Must construct a QApplication before a QPaintDevice crash error

    Quote Originally Posted by high_flyer View Post
    Under windows at least you do get a handle to your DLL.
    Only if you load it explicitly. Not if it's loaded implicitly by the linker. Try writing a simple application that links with QtCore and unload that library while running the program. In other words, modify this code:

    Qt Code:
    1. #include <stdio.h>
    2.  
    3. int main(int argc, char **argv) {
    4. getchar();
    5. return 0;
    6. }
    To copy to clipboard, switch view to plain text mode 

    qmake Code:
    1. SOURCES += main.cpp
    2. QT = core
    To copy to clipboard, switch view to plain text mode 

    ... and make it unload QtCore before the call to getchar().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  19. #19
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QWidget: Must construct a QApplication before a QPaintDevice crash error

    That is correct - but plugins are explicitly loaded, at least when I implement them, that is the whole point with plugins.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  20. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QWidget: Must construct a QApplication before a QPaintDevice crash error

    That's why I said that all depends on whether the plugin is explicitly unloaded or not. If not then you can't unload Qt that is implicitly loaded by the plugin.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 16
    Last Post: 12th December 2014, 20:22
  2. Replies: 4
    Last Post: 27th December 2011, 11:34
  3. Replies: 2
    Last Post: 25th July 2011, 08:08
  4. Replies: 0
    Last Post: 20th April 2009, 17:45
  5. Must construct QApplication before QPaintDevice
    By sekatsim in forum Qt Programming
    Replies: 16
    Last Post: 8th June 2008, 00:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.