Results 1 to 6 of 6

Thread: Crash application after delete window

  1. #1
    Join Date
    Aug 2009
    Posts
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Crash application after delete window

    I have interesting example. We are have qml form, and button. After click need close window and delete window. Simple. But application stable crash. Example:

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2.  
    3. #include "windowmanager.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. * *QApplication a(argc, argv);
    8.  
    9. * *WindowManager manager;
    10. * *manager.showWindow();
    11.  
    12. * *return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    windowmanager.h
    Qt Code:
    1. #ifndef WINDOWMANAGER_H
    2. #define WINDOWMANAGER_H
    3.  
    4. #include <QObject>
    5. #include <QDeclarativeView>
    6.  
    7. class WindowManager : public QObject
    8. {
    9. * *Q_OBJECT
    10. * *public:
    11. * * * *explicit WindowManager(QObject *parent = 0);
    12.  
    13. * * * *void showWindow();
    14. * * * *Q_INVOKABLE void closeWindow();
    15.  
    16. * * * *QDeclarativeView *window();
    17.  
    18. * *private:
    19. * * * *QDeclarativeView *m_window;
    20.  
    21. };
    22.  
    23. #endif // WINDOWMANAGER_H
    To copy to clipboard, switch view to plain text mode 


    windowmanager.cpp
    Qt Code:
    1. #include "windowmanager.h"
    2.  
    3. #include <QDeclarativeContext>
    4.  
    5. WindowManager::WindowManager(QObject *parent) : QObject(parent), m_window(0)
    6. {
    7. }
    8.  
    9. void WindowManager::showWindow()
    10. {
    11. * *window()->show();
    12. }
    13.  
    14. void WindowManager::closeWindow()
    15. {
    16. * *m_window->close();
    17.  
    18. * *if (m_window != 0) {
    19. * * * *delete m_window;
    20. * * * *m_window = 0;
    21. * *}
    22. }
    23.  
    24. QDeclarativeView *WindowManager::window()
    25. {
    26. * *if (m_window == 0) {
    27. * * * *m_window = new QDeclarativeView(0);
    28.  
    29. * * * *m_window->rootContext()->setContextProperty("manager",this);
    30. * * * *m_window->setSource(QUrl("qrc:/main.qml"));
    31. * *}
    32.  
    33. * *return m_window;
    34. }
    To copy to clipboard, switch view to plain text mode 

    main.qml
    Qt Code:
    1. import QtQuick 1.1
    2.  
    3. Item {
    4. * *width: 500
    5. * *height: 500
    6.  
    7. * *Rectangle {
    8. * * * *width: 200
    9. * * * *height: 200
    10.  
    11. * * * *anchors.centerIn: parent
    12.  
    13. * * * *color: "red"
    14.  
    15. * * * *Text {
    16. * * * * * *anchors.centerIn: parent
    17. * * * * * *color: "white"
    18.  
    19. * * * * * *text: "Click for crash"
    20. * * * *}
    21.  
    22. * * * *MouseArea {
    23. * * * * * *anchors.fill: parent
    24.  
    25. * * * * * *onClicked: {
    26. * * * * * * * *manager.closeWindow();
    27. * * * * * *}
    28. * * * *}
    29. * *}
    30. }
    To copy to clipboard, switch view to plain text mode 

    If i use, deleteLater() then applicaition crash in 20% users. If i user hotkey for delete window, all work is good, but why app crash in click button?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Crash application after delete window

    I would definitely use deleteLater() since otherwise you are basically doing a "delete this" (code in Window and its QML engine still being executed at time of delete).

    You will have to debug the crash to see where it happens, though.
    E.g. run the application in a debugger.

    The user closing the window manually is different, as this doesn't delete the window.

    Cheers,
    _

  3. #3
    Join Date
    Aug 2009
    Posts
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Crash application after delete window

    Initially im use deleteLater() and in 80% of cases application work is wery good. But on mac os 20% users catch crash. delete - help for me localization problem. In debug i see, application crash in

    QCoreApplication::notifyInternal(QObject *, QEvent *)

    and delete and deleteLater().

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Crash application after delete window

    Hmm, that is indeed not a lot to go on.

    Do you need to delete the window?

    I.e. it is not being deleted when the user closes it, so I am wondering why you need to delete it when you are closing it from code.

    Cheers,
    _

  5. #5
    Join Date
    Aug 2009
    Posts
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Crash application after delete window

    I`m find solution. I`m delete window not after close() are before show()

    Qt Code:
    1. if (window != 0) {
    2. delete window;
    3. window = 0;
    4. }
    5.  
    6. window = new QDeclarativeView();
    7. window->show();
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Crash application after delete window

    you can call delete window even without the check for != 0 first,
    Delete is defined to do nothing on a null pointer.

    Cheers,
    _

Similar Threads

  1. Replies: 0
    Last Post: 15th October 2013, 09:32
  2. Crash in editorEvent with a delete key press event
    By xiangyu in forum Qt Programming
    Replies: 0
    Last Post: 26th March 2013, 15:57
  3. Replies: 3
    Last Post: 21st July 2010, 07:40
  4. delete runtime-created widgets causes crash
    By jonasbalmer in forum Qt Programming
    Replies: 5
    Last Post: 10th February 2010, 21:36
  5. crash when delete QTreeWidgetItem*
    By evgenM in forum Qt Programming
    Replies: 9
    Last Post: 9th December 2006, 20:04

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.