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. Tests crash when using C++ Singletons
Forum Updated to NodeBB v4.3 + New Features

Tests crash when using C++ Singletons

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 4 Posters 485 Views 1 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.
  • P Offline
    P Offline
    PavloPonomarov
    wrote on 6 Oct 2022, 13:22 last edited by
    #1

    Hello,
    I have two QML components that use same singleton object and I'm trying to test them. Singleton is registered via qmlRegisterSingletonType() in main.cpp. Each test file separately works fine, but the tests crashes when I run both tests. After some digging I found out that cleanUpTestcase() from first test deletes the singleton and crash happens when second component calls the singleton object.
    I also found very similar problem reported as bug here, but the workaround suggested by assignee doesn't work for me. When registered, singleton always logs that it has CppOwnership:

    QObject* SingletonObject::register(QQmlEngine *engine, QJSEngine *scriptEngine)
    {
        static SingletonObject* self = new SingletonObject();
        qDebug()<<"[SingletonObject]"<<(engine->objectOwnership(self) ? "JsOwnership" : "CppOwnership");
        return self;
    }
    

    Which means QQmlEngine::setObjectOwnership(instance, QQmlEngine::CppOwnership); will do nothing in my case.
    Is there any other option to avoid this crash?

    J G 2 Replies Last reply 6 Oct 2022, 13:29
    0
    • P PavloPonomarov
      6 Oct 2022, 13:22

      Hello,
      I have two QML components that use same singleton object and I'm trying to test them. Singleton is registered via qmlRegisterSingletonType() in main.cpp. Each test file separately works fine, but the tests crashes when I run both tests. After some digging I found out that cleanUpTestcase() from first test deletes the singleton and crash happens when second component calls the singleton object.
      I also found very similar problem reported as bug here, but the workaround suggested by assignee doesn't work for me. When registered, singleton always logs that it has CppOwnership:

      QObject* SingletonObject::register(QQmlEngine *engine, QJSEngine *scriptEngine)
      {
          static SingletonObject* self = new SingletonObject();
          qDebug()<<"[SingletonObject]"<<(engine->objectOwnership(self) ? "JsOwnership" : "CppOwnership");
          return self;
      }
      

      Which means QQmlEngine::setObjectOwnership(instance, QQmlEngine::CppOwnership); will do nothing in my case.
      Is there any other option to avoid this crash?

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 6 Oct 2022, 13:29 last edited by
      #2

      @PavloPonomarov said in Tests crash when using C++ Singletons:

      When registered, singleton always logs that it has CppOwnership

      at this moment in time?, yes true, as soon as the function actually returns and the pointer is passed to the QMLEngine, the object ownership changes.

      That also means, you'll have to set ownership after the QMLEngine has taken over.

      the hacky way I do/did this is/was:

      static QObject *instance(QQmlEngine *engine, QJSEngine *) {
             ClimateSetting = new ClimateSettings();
              QMetaObject::invokeMethod(ClimateSetting, [=]()->void{
                  engine->setObjectOwnership(ClimateSetting, QQmlEngine::CppOwnership);
              }, Qt::QueuedConnection);
              return  ClimateSetting;
      }
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      2
      • F Offline
        F Offline
        fcarney
        wrote on 6 Oct 2022, 15:15 last edited by
        #3

        @J-Hilk said in Tests crash when using C++ Singletons:

        QMetaObject::invokeMethod(ClimateSetting, =->void{
        engine->setObjectOwnership(ClimateSetting, QQmlEngine::CppOwnership);
        }, Qt::QueuedConnection);

        If I am reading the docs right, this looks like a way to do a callLater on the object. Did not know you could do that from C++. Thanks for sharing.

        C++ is a perfectly valid school of magic.

        1 Reply Last reply
        0
        • P PavloPonomarov
          6 Oct 2022, 13:22

          Hello,
          I have two QML components that use same singleton object and I'm trying to test them. Singleton is registered via qmlRegisterSingletonType() in main.cpp. Each test file separately works fine, but the tests crashes when I run both tests. After some digging I found out that cleanUpTestcase() from first test deletes the singleton and crash happens when second component calls the singleton object.
          I also found very similar problem reported as bug here, but the workaround suggested by assignee doesn't work for me. When registered, singleton always logs that it has CppOwnership:

          QObject* SingletonObject::register(QQmlEngine *engine, QJSEngine *scriptEngine)
          {
              static SingletonObject* self = new SingletonObject();
              qDebug()<<"[SingletonObject]"<<(engine->objectOwnership(self) ? "JsOwnership" : "CppOwnership");
              return self;
          }
          

          Which means QQmlEngine::setObjectOwnership(instance, QQmlEngine::CppOwnership); will do nothing in my case.
          Is there any other option to avoid this crash?

          G Offline
          G Offline
          GrecKo
          Qt Champions 2018
          wrote on 6 Oct 2022, 15:55 last edited by GrecKo 10 Jun 2022, 15:56
          #4

          @PavloPonomarov said in Tests crash when using C++ Singletons:

          When registered, singleton always logs that it has CppOwnership:
          Which means QQmlEngine::setObjectOwnership(instance, QQmlEngine::CppOwnership); will do nothing in my case.

          Not really, CppOwnership is also returned for objects with no explicit ownership. If you were to set it explicitely, it won't be overwritten later by the engine.

          1 Reply Last reply
          3

          1/4

          6 Oct 2022, 13:22

          • Login

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