Important: Please read the Qt Code of Conduct -

QML object implemented in C++ does not call the destructor

  • I have a QML object (CppObject) implemented in C++ which is then registered with qmlRegisterType.
    It is instantiated in QML document without any problem, but when the application is closed (Qt.quit()), the C++ destructor of CppObject class is not called.

    I'm using Qt 5.7 and Quick Controls 2.

    Any idea, how to fix his problem?

  • Hard to say without seeing any code. :) Are you using QQmlApplicationEngine to load the QML code? Is it allocated on the stack in main()?

  • Qt Champions 2017

    It works and we are using it. Do you have destructor ? See the sample here.


        id : log
    MouseArea {
        anchors.fill: parent
        onClicked: {
            console.log("I am quitting")
            log.destroy()// Without calling this destroy also it works.

    qDebug() << Q_FUNC_INFO << " Constructor " <<endl;

    void Logging::callme(){
    qDebug() << Q_FUNC_INFO << " Called me " <<endl;

    qDebug() << Q_FUNC_INFO << " Destructor " <<endl;


  • I have a test case similar as your implementation and the destructor is called only when I use destroy() as your example.
    The moment I don't call destroy(), the destructor is not called.

  • @dmendizabal Hi! Please provide a complete minimal example so that we can try to reproduce the behaviour.

  • @Wieland Please find example below:


    #include <QtQuick>
    #include "message.h"
    int main (int argc, char *argv[])
      QGuiApplication app(argc, argv);
      qmlRegisterType<CMessage>("com.message", 1, 0, "Message");
      QQmlApplicationEngine engine;
      return app.exec();

    message.h ---> QML object implemented in C++

    #include <QObject>
    class CMessage : public QObject
      explicit CMessage(QObject *parent = 0);

    message.cpp ---> QML object implementation in C++

    #include <QtGui>
    #include "message.h"
    CMessage::CMessage(QObject *parent) : QObject(parent)

    main.qml ---> QML main window

    import QtQuick 2.7
    import QtQuick.Controls 2.0
    import QtQuick.Layouts 1.3
    ApplicationWindow {
        visible: true
        width: 640
        height: 480
        StackView {
            id: id_stack
            anchors.fill: parent
            initialItem: "qrc:page1.qml"

    page1.qml ---> QML page

    import QtQuick 2.5
    import QtQuick.Controls 2.0
    import QtQuick.Layouts 1.3
    import QtQuick.Window 2.0
    import com.message 1.0
    Page {
        id: id_item
        header: ToolBar {
            id: id_toolbar
            RowLayout {
                id: id_layout
                ToolButton {
                    id: id_quitButton
                    text: qsTr("Quit")
                    onClicked: Qt.quit()
        Message {
            id: id_message

  • @dmendizabal Thank you for the test case. This is StackView's fault for not claiming proper ownership of a dynamically instantiated initial item. The leak has been fixed in Qt 5.7.1.

  • @jpnurmi Thanks.
    I will wait for 5.7.1 and it may resolve also another problem I'm facing that looks to be in relation to StackView.

  • @dmendizabal What is the other problem?

Log in to reply