Results 1 to 20 of 20

Thread: Crash when showing windows

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2011
    Posts
    19
    Thanks
    4

    Default Crash when showing windows

    Hello,

    I have a problem when my program starts. It randomly crash (it's not always)... I can run it 10 times correctly but i can also run it 2 times with crashs....

    I don't understand what is the problem, it is a random bug, i tryied to put cout on the source, in order to know where it is crash, and it's not at the same "point" every times......

    The main cpp is :
    Qt Code:
    1. VerifFiles verif;
    2. verif.exec();
    3. CZoneDessin fen(0, 0);
    4. fen.show();
    To copy to clipboard, switch view to plain text mode 

    If i do :
    Qt Code:
    1. CZoneDessin fen(0, 0);
    2. fen.show();
    To copy to clipboard, switch view to plain text mode 
    The program never crashs with that..

    So i think the problem is during the first windows closing and the new is creating... Maybe my syntax isn't correct, or something i do wrong...

    The first windows checks some files and if it is ok, the windows will be closed. If not, it will return exit(0), so the program will be closed.
    When the first windows (verif) close, i create an instance of czonedessin and i show it (it is the main program).

    I can show directly the czonedessin window, but i MUST check somes files before ! So it's why i create an "intermediate" window that do it..

    How can i test that, have you an idea ? I don't understand and i think i can't put all the code here because czonedessin have 1222 lines, and a lot of includes .h

    Thank's in advance for your time, and i'm very sorry for my bad english,

  2. #2
    Join Date
    Mar 2011
    Location
    Denmark
    Posts
    74
    Qt products
    Qt4
    Platforms
    MacOS X Windows
    Thanks
    7
    Thanked 10 Times in 9 Posts

    Default Re: Crash when showing windows

    Quote Originally Posted by Selven View Post
    I don't understand what is the problem, it is a random bug
    I doubt it's random it sounds more like a race condition.

    Your code for main.cpp looks wierd to me, where is you instance of QApplication? Is VerifFiles a modal dialogue?

    You never check the result of VerifFiles before instantiating CZoneDessin, I assume you only want to show it if verification passed

    By default QApplication will shut down if no windows are open, this means it will quit after VerifFiles is closed and before CZoneDessin is created to avoid this you need to make sure you set

    Qt Code:
    1. QApplication::setQuitOnLastWindowClosed(false)
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2011
    Posts
    19
    Thanks
    4

    Default Re: Crash when showing windows

    Hi,
    I quote only the minimal part of the main.cpp.
    The code is (without include) :
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4. VerifFiles verif;
    5. verif.exec();
    6. CZoneDessin fen(0, 0);
    7. fen.show();
    8. return app.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 

    The crash is really randomly, it don't crash every times... So it's why it's very strange...

    VerifFyles is herited from QDialog, czonedessin is herited from QLabel..
    I have a button on my verifFyles windows, connected to a slot. When i push it, i verify somes files and if it's good, i close() the window. So, the main.cpp continue and launch the czonedessin window. If an error occured, when i push the button i detect it and i exit(0). So, it quit the application, not just the window.

    When a crash occured, it's when creating the czonedessin instance...

    Thank's for your help,

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,332
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    317
    Thanked 871 Times in 858 Posts

    Default Re: Crash when showing windows

    Since you haven't told us anything about the classes in your application or what they do, should we just guess at answers until we get lucky?

    - First: If VerifFiles is a GUI class or something else that uses events, nothing will happen until the call to app.exec(). The event loop is not running until that point. So, the call to verif.exec() is only posting events (just like fen.show()), but nothing happens until after the call to app.exec().

    - Second: If CZoneDessin somehow depends on what happens in VerifFiles::exec() (or depends on it finishing before being constructed), then this won't happen in your code. Both the VerifFiles and CZoneDessin instances will be constructed, the call to verif.exec() will result in events being posted to the event queue, as will the events involved in fen.show(). Who knows what order these will get processed in?

    From other posts you have made, you seem to have a very basic misunderstanding of how Qt and the Qt event loop work. As I said above, nothing happens until app.exec(). It might look like it is, but in reality all that is happening is that your code is putting things in the event queue to be performed after the event loop gets started.

    For this reason, if you look at almost any of the Qt example apps, the only things that typically occur are:

    - Create a QApplication instance on the stack
    - Create an instance of the app's main window widget class on the stack
    - Show the main window (which simply queues up a show event and associated processing)
    - app.exec()

    If you try to do anything else in main() that requires a GUI, it probably won't work, and will lead to weird behavior like you are seeing.

    So, you need to restructure your app somehow so that events happen in the order that you expect them to. Like a previous poster said, if the VerifFiles window closes before the CZoneDessin window is shown, then the application could shut down too early. If the CZoneDessin widget is partially destroyed at that point, then that could be the cause of your random crashes.

  5. The following user says thank you to d_stranz for this useful post:

    Selven (11th April 2012)

  6. #5
    Join Date
    Jan 2011
    Posts
    19
    Thanks
    4

    Default Re: Crash when showing windows

    Hello,

    Thank's a lot for your response,

    I think i have another way to do what i want. So in the main.cpp i will just have :
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4. CZoneDessin fen;
    5. fen.show();
    6. return app.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 

    And in the constructor of czonedessin, on the top, as the first instruction i put my instance of VerifFiles. So, if the result is good, i continue the constructor, if not, i do exit(0) and so, the program will be closed.

    Do you think it will works better ?

    Edit : i try that and i have somes crash..... same issue....
    Last edited by Selven; 11th April 2012 at 16:03.

  7. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,332
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    317
    Thanked 871 Times in 858 Posts

    Default Re: Crash when showing windows

    And in the constructor of czonedessin, on the top, as the first instruction i put my instance of VerifFiles. So, if the result is good, i continue the constructor, if not, i do exit(0) and so, the program will be closed.

    Do you think it will works better ?
    No, it will not work any better. In the constructor for CZoneDessin, the event loop is not running. Like I said, nothing happens until the event loop runs. So, you build your CZoneDessing instance, the constructor exits, then you are back in main() where you then show() the window. Even then, still nothing happens until the call to app.exec().

    So, think about what your new code does:

    - It creates a CZoneDessin instance.
    - Inside the CZoneDessing constructor, it creates a VerifFiles instance on the stack.
    - It calls VerifFiles::exec() with that instance. Remember, all this does is to post events to the queue, but since the event loop isn't running yet, nothing happens on screen yet.
    - Because there is no event loop, VerifFiles::exec() returns immediately and the CZoneDessin constructor eventually exits
    - When that happens, the VerifFiles instance is destroyed. The events are still on the event queue, but now they refer to an object that is gone.
    - Back in main(), you call fen.show() and app.exe()
    - The event loop starts, and BOOM!, your program blows up.

    So, what you probably want to do is this:

    - Implement a handler for the CZoneDessin::showEvent().
    - In the CZoneDessin constructor, build your GUI as if everything was normal.
    - Move the VerifFiles code to the showEvent()
    - In showEvent, create the VerifFiles instance, call exec(), and if it fails, show a fatal error message to the user, then call close(). This will cause the application to exit.
    - If VerifFiles succeeds, do nothing and the CZoneDessin widget will be displayed.

    Something like this:

    Qt Code:
    1. // CZoneDessin.h:
    2.  
    3. class CZoneDessin : public QWidget
    4. {
    5. Q_OBJECT;
    6.  
    7. //...
    8.  
    9. protected:
    10. void showEvent( QShowEvent * );
    11. };
    12.  
    13. // CZoneDessin.cpp:
    14. void CZoneDessin::showEvent( QShowEvent * )
    15. {
    16. VerifFiles verif;
    17. if ( QDialog::Rejected == verif.exec() )
    18. close();
    19. }
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to d_stranz for this useful post:

    Selven (11th April 2012)

  9. #7
    Join Date
    Jan 2011
    Posts
    19
    Thanks
    4

    Default Re: Crash when showing windows

    Okay, now i understand how the event loop works, thanks a lot.

    On my .h i added the void showEvent, and in the .cpp, I put the instance and exec in this method, like you quoted.

    I have random crashes again... Maybe the problem is elsewhere.... A bad pointer or a problem with memory (I don't use malloc). But I don't understand that : if I remove the veriffiles instance and if I launch the czonedessin directly, I don't have any crashes..

    Maybe if you have time and if you are interested with that, i can give you the project, but i don't think you want to loose time with that.... ^^
    But i don't want abuse and ask for too much,

    Thanks for your help, all your responses are very helpful for me !

  10. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,332
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    317
    Thanked 871 Times in 858 Posts

    Default Re: Crash when showing windows

    OK, it sounds like the problem is inside of the VerifFiles dialog class. Try an experiment: instead of using VerifFiles, use one of the existing Qt dialog classes, like QColorDialog.

    Qt Code:
    1. // CZoneDessin.cpp:
    2. void CZoneDessin::showEvent( QShowEvent * )
    3. {
    4. QColorDialog dlg( this );
    5. if ( QDialog::Rejected == dlg.exec() )
    6. {
    7. QMessageBox::information( this, "Rejected", "QColorDialog rejected" );
    8. close();
    9. }
    10. else
    11. QMessageBox::information( this, "Accepted", "QColorDialog accepted" );
    12. }
    To copy to clipboard, switch view to plain text mode 

    The QColorDialog will have OK and Cancel buttons; clicking Cancel with display the "Rejected" message box, clicking OK will display the "Accepted" message box.

    If this works without crashing, then the problem is almost certainly inside your VerifFiles class (unless you have a memory corruption before that somewhere).

  11. The following user says thank you to d_stranz for this useful post:

    Selven (11th April 2012)

  12. #9
    Join Date
    Jan 2011
    Posts
    19
    Thanks
    4

    Default Re: Crash when showing windows

    Hi,

    With your example, the program crashes again but LESS !! In 50 runs, only 3 crashes !
    Something is strange : when I click Rejected, the windows closes but the application is still running. However, the attribute app.setQuitOnLastWindowClosed is by default at true.. I don't change it.

    My VerifFiles is closed manually : i have a button connected to a slot, when i push it, the slot (lancerApp()) update a bool and do a close() for close the window. How can i change the value of the exec for test it ? Because it's not an Accepted/Rejected dialog buttons.
    Qt Code:
    1. void VerifFiles::lancerApp()
    2. {
    3. confOK = true;
    4. close();
    5. }
    To copy to clipboard, switch view to plain text mode 

    So, there is a bug in VerifFiles, and also in CZoneDessin ?

    Thanks a lot for all your help,

  13. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,332
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    317
    Thanked 871 Times in 858 Posts

    Default Re: Crash when showing windows

    The usual behavior in a custom QDialog class is to emit one of the two signals "accepted()" or "rejected()" when the dialog is done. If you do not have OK or Cancel buttons, then you should emit those signals when VerifFiles determines if the result is successful or failed. If you use QDialog::exec() to post the dialog, you should not call "close()" from inside the dialog to close it, let the normal accept/reject mechanism do it for you.

    Qt Code:
    1. // VerifFiles.cpp
    2. void VerifFiles::lancerApp()
    3. {
    4. emit accepted();
    5. }
    6.  
    7. // CZoneDessin.cpp
    8. void CZoneDessin::showEvent( QShowEvent * )
    9. {
    10. VerifFiles verif;
    11. if ( QDialog::Rejected == verif.exec() )
    12. close();
    13. }
    To copy to clipboard, switch view to plain text mode 

    If you have to have a different code than QDialog::Accepted or QDialog::Rejected, you can emit the "finished( int )" signal instead and put your own integer code in there:

    Qt Code:
    1. // VerifFiles.cpp
    2. void VerifFiles::lancerApp()
    3. {
    4. emit finished( 42 );
    5. }
    6.  
    7. // CZoneDessin.cpp
    8. void CZoneDessin::showEvent( QShowEvent * )
    9. {
    10. VerifFiles verif;
    11. if ( 42 == verif.exec() )
    12. close();
    13. }
    To copy to clipboard, switch view to plain text mode 

  14. The following user says thank you to d_stranz for this useful post:

    Selven (11th April 2012)

  15. #11
    Join Date
    Jan 2011
    Posts
    19
    Thanks
    4

    Default Re: Crash when showing windows

    Qt Code:
    1. //VerifFiles.cpp
    2. void VerifFiles::lancerApp()
    3. {
    4. emit accepted();
    5. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //CzoneDessin.cpp
    2. void CZoneDessin::showEvent(QShowEvent *)
    3. {
    4. // Affichage de la fenêtre de vérification des fichiers / Auto-configuration
    5. VerifFiles testFichiers;
    6. if(QDialog::Accepted != testFichiers.exec())
    7. close();
    8. }
    To copy to clipboard, switch view to plain text mode 

    The emit accepted() seems not close the window when I click the button. I tryied to put a cout on the lancerApp() slots, and the message is successfully displayed.. But the window is still visible and hold the program (CZoneDessin will not be visible while this window is visible)

    And the close() in CZoneDessin::showEvent don't close the application, but only the window CZoneDessin (which is not visible at this step). Can i use exit(0) instead close() ?

    About crashes, so it's VerifFiles which doing crashes the program ? Without this class, I have always crashes but much less !
    VerifFiles analyses 200 files, this class have 400 lines and i can't put them here, it will take too much place..

    Thanks for the quality of all your responses

    PS : is it not too hard to understand me ? My english is so bad !
    Last edited by Selven; 11th April 2012 at 21:12.

Similar Threads

  1. Replies: 0
    Last Post: 20th February 2012, 05:39
  2. Replies: 4
    Last Post: 15th September 2011, 14:09
  3. qFileDialog showing two browse windows on Qt4.7
    By papillon in forum Qt Programming
    Replies: 2
    Last Post: 7th September 2011, 17:16
  4. Qt .qrc ressources showing on Windows 7 but not on Windows XP
    By The_Student in forum Qt Programming
    Replies: 1
    Last Post: 8th February 2011, 09:53
  5. The direction of the showing of windows.
    By Mad Max in forum Qt Programming
    Replies: 6
    Last Post: 12th January 2007, 12:15

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.