Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Initializing the QT MetaObject system in QT5
Forum Updated to NodeBB v4.3 + New Features

Initializing the QT MetaObject system in QT5

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 346 Views
  • 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.
  • A Offline
    A Offline
    AndrewCQT
    wrote on last edited by
    #1

    Hi,
    I am (ab)using the QT meta object system to initialize static objects in my .CPP files (OS X, QT 5.15.2)

    For example I am calling staticMetaObject.className() of a class in a .cpp file, for example ( abbreviated for clarity)

    static QString classname=staticMetaObject.className();

    I am running afoul of C++ static initialization order. That is the static metaObject data stored in a generated moc_<classname>.cpp file is not initialized when the static member in "my". cpp is called so staticMetaObject.className() is garbage and the software crashes.

    If, for example, I move my so-called "static" initialization to a function called after main() starts , then all is good because all the moc_ metadata is initialized and present.

    Oddly this is a problem in QT5, not QT6 but my code has to run in both QT5 and QT6

    Andrew

    Christian EhrlicherC 1 Reply Last reply
    0
    • A AndrewCQT

      Hi,
      I am (ab)using the QT meta object system to initialize static objects in my .CPP files (OS X, QT 5.15.2)

      For example I am calling staticMetaObject.className() of a class in a .cpp file, for example ( abbreviated for clarity)

      static QString classname=staticMetaObject.className();

      I am running afoul of C++ static initialization order. That is the static metaObject data stored in a generated moc_<classname>.cpp file is not initialized when the static member in "my". cpp is called so staticMetaObject.className() is garbage and the software crashes.

      If, for example, I move my so-called "static" initialization to a function called after main() starts , then all is good because all the moc_ metadata is initialized and present.

      Oddly this is a problem in QT5, not QT6 but my code has to run in both QT5 and QT6

      Andrew

      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @AndrewCQT said in Initializing the QT MetaObject system in QT5:

      Oddly this is a problem in QT5, not QT6 but my code has to run in both QT5 and QT6

      Just by accident - the order is not defined.

      Re-write your code to not use those global statics.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      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