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 461 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 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.HilkJ GrecKoG 2 Replies Last reply
    0
    • P PavloPonomarov

      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.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on 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
      • fcarneyF Offline
        fcarneyF Offline
        fcarney
        wrote on 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

          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?

          GrecKoG Offline
          GrecKoG Offline
          GrecKo
          Qt Champions 2018
          wrote on last edited by GrecKo
          #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

          • Login

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