PDA

View Full Version : Crash application after delete window



ecspertiza
28th September 2016, 10:27
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


#include <QApplication>

#include "windowmanager.h"

int main(int argc, char *argv[])
{
* *QApplication a(argc, argv);

* *WindowManager manager;
* *manager.showWindow();

* *return a.exec();
}


windowmanager.h


#ifndef WINDOWMANAGER_H
#define WINDOWMANAGER_H

#include <QObject>
#include <QDeclarativeView>

class WindowManager : public QObject
{
* *Q_OBJECT
* *public:
* * * *explicit WindowManager(QObject *parent = 0);

* * * *void showWindow();
* * * *Q_INVOKABLE void closeWindow();

* * * *QDeclarativeView *window();

* *private:
* * * *QDeclarativeView *m_window;

};

#endif // WINDOWMANAGER_H



windowmanager.cpp


#include "windowmanager.h"

#include <QDeclarativeContext>

WindowManager::WindowManager(QObject *parent) : QObject(parent), m_window(0)
{
}

void WindowManager::showWindow()
{
* *window()->show();
}

void WindowManager::closeWindow()
{
* *m_window->close();

* *if (m_window != 0) {
* * * *delete m_window;
* * * *m_window = 0;
* *}
}

QDeclarativeView *WindowManager::window()
{
* *if (m_window == 0) {
* * * *m_window = new QDeclarativeView(0);

* * * *m_window->rootContext()->setContextProperty("manager",this);
* * * *m_window->setSource(QUrl("qrc:/main.qml"));
* *}

* *return m_window;
}


main.qml


import QtQuick 1.1

Item {
* *width: 500
* *height: 500

* *Rectangle {
* * * *width: 200
* * * *height: 200

* * * *anchors.centerIn: parent

* * * *color: "red"

* * * *Text {
* * * * * *anchors.centerIn: parent
* * * * * *color: "white"

* * * * * *text: "Click for crash"
* * * *}

* * * *MouseArea {
* * * * * *anchors.fill: parent

* * * * * *onClicked: {
* * * * * * * *manager.closeWindow();
* * * * * *}
* * * *}
* *}
}


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?

anda_skoa
28th September 2016, 11:12
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,
_

ecspertiza
28th September 2016, 11:34
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().

anda_skoa
28th September 2016, 12:39
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,
_

ecspertiza
20th October 2016, 17:00
I`m find solution. I`m delete window not after close() are before show()



if (window != 0) {
delete window;
window = 0;
}

window = new QDeclarativeView();
window->show();

anda_skoa
20th October 2016, 17:36
you can call delete window even without the check for != 0 first,
Delete is defined to do nothing on a null pointer.

Cheers,
_