Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML object implemented in C++ does not call the destructor
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
9 Posts 4 Posters 4.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dmendizabal
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • jpnurmiJ Offline
      jpnurmiJ Offline
      jpnurmi
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

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

        QML

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

        C++
        Logging::Logging()
        {
        qDebug() << Q_FUNC_INFO << " Constructor " <<endl;
        }

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

        Logging::~Logging(){
        qDebug() << Q_FUNC_INFO << " Destructor " <<endl;
        }

        main.cpp
        qmlRegisterType<Logging>("Log",1,0,"MyLog");

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        D 1 Reply Last reply
        4
        • dheerendraD dheerendra

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

          QML

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

          C++
          Logging::Logging()
          {
          qDebug() << Q_FUNC_INFO << " Constructor " <<endl;
          }

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

          Logging::~Logging(){
          qDebug() << Q_FUNC_INFO << " Destructor " <<endl;
          }

          main.cpp
          qmlRegisterType<Logging>("Log",1,0,"MyLog");

          D Offline
          D Offline
          dmendizabal
          wrote on last edited by
          #4

          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.

          ? 1 Reply Last reply
          0
          • D dmendizabal

            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.

            ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

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

            D 1 Reply Last reply
            1
            • ? A Former User

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

              D Offline
              D Offline
              dmendizabal
              wrote on last edited by
              #6

              @Wieland Please find example below:

              main.cpp

              #include <QtQuick>
              #include "message.h"
              
              int main (int argc, char *argv[])
              {
                QGuiApplication app(argc, argv);
                qmlRegisterType<CMessage>("com.message", 1, 0, "Message");
              
                QQmlApplicationEngine engine;
                engine.load(QUrl(QLatin1String("qrc:/main.qml")));
              
                return app.exec();
              }
              

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

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

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

              #include <QtGui>
              #include "message.h"
              
              CMessage::CMessage(QObject *parent) : QObject(parent)
              {
               qDebug()<<"Constructor";
              }
              
              CMessage::~CMessage()
              {
                qDebug()<<"Destructor";
              }
              

              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
                  }
              }
              
              jpnurmiJ 1 Reply Last reply
              1
              • D dmendizabal

                @Wieland Please find example below:

                main.cpp

                #include <QtQuick>
                #include "message.h"
                
                int main (int argc, char *argv[])
                {
                  QGuiApplication app(argc, argv);
                  qmlRegisterType<CMessage>("com.message", 1, 0, "Message");
                
                  QQmlApplicationEngine engine;
                  engine.load(QUrl(QLatin1String("qrc:/main.qml")));
                
                  return app.exec();
                }
                

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

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

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

                #include <QtGui>
                #include "message.h"
                
                CMessage::CMessage(QObject *parent) : QObject(parent)
                {
                 qDebug()<<"Constructor";
                }
                
                CMessage::~CMessage()
                {
                  qDebug()<<"Destructor";
                }
                

                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
                    }
                }
                
                jpnurmiJ Offline
                jpnurmiJ Offline
                jpnurmi
                wrote on last edited by
                #7

                @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.

                D 1 Reply Last reply
                1
                • jpnurmiJ jpnurmi

                  @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.

                  D Offline
                  D Offline
                  dmendizabal
                  wrote on last edited by
                  #8

                  @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.

                  jpnurmiJ 1 Reply Last reply
                  0
                  • D dmendizabal

                    @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.

                    jpnurmiJ Offline
                    jpnurmiJ Offline
                    jpnurmi
                    wrote on last edited by
                    #9

                    @dmendizabal What is the other problem?

                    1 Reply Last reply
                    1

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved