Segfault after removal of Q_OBJECT macro - what "other services" may fail
-
The QObject doc page says: »The Q_OBJECT macro must appear in the private section of a class definition that declares its own signals and slots or that uses other services provided by Qt's meta-object system.«
Is there an authorative list of these »other services«? How do I see from a class definition whether any of them are used and require the Q_OBJECT macro?
In a big project, I removed the
Q_OBJECT
from class definitions if the class contains neither signals nor slots, nor makes use of .ui files. This went well in most cases, but in a few cases led to segfaults.Is it normal and expected behavior that missing Q_OBJECT macro is not detected at compile or link time, but corrupts the runtime?
-
The QObject doc page says: »The Q_OBJECT macro must appear in the private section of a class definition that declares its own signals and slots or that uses other services provided by Qt's meta-object system.«
Is there an authorative list of these »other services«? How do I see from a class definition whether any of them are used and require the Q_OBJECT macro?
In a big project, I removed the
Q_OBJECT
from class definitions if the class contains neither signals nor slots, nor makes use of .ui files. This went well in most cases, but in a few cases led to segfaults.Is it normal and expected behavior that missing Q_OBJECT macro is not detected at compile or link time, but corrupts the runtime?
@Joachim-W said in Segfault after removal of Q_OBJECT macro - what "other services" may fail:
Is it normal and expected behavior that missing Q_OBJECT macro is not detected at compile or link time, but corrupts the runtime?
Yes - how should e.g. the meta object system know something about if a custom class has a Q_OBJECT macro or not?
All this won't work: https://doc.qt.io/qt-6/qmetaobject.html and also see https://doc.qt.io/qt-6/qobject.html#qobject_cast which is a common usecase for a QObject derived class (but it will fail during compile time when the class to cast to has no Q_OBJECT)
/edit: and when there is a segfault you should use a debugger to see where it crashes.
-
@Joachim-W said in Segfault after removal of Q_OBJECT macro - what "other services" may fail:
Is it normal and expected behavior that missing Q_OBJECT macro is not detected at compile or link time, but corrupts the runtime?
Yes - how should e.g. the meta object system know something about if a custom class has a Q_OBJECT macro or not?
All this won't work: https://doc.qt.io/qt-6/qmetaobject.html and also see https://doc.qt.io/qt-6/qobject.html#qobject_cast which is a common usecase for a QObject derived class (but it will fail during compile time when the class to cast to has no Q_OBJECT)
/edit: and when there is a segfault you should use a debugger to see where it crashes.
From the QMetaObject doc: »This class is not normally required for application programming«
-
@Joachim-W said in Segfault after removal of Q_OBJECT macro - what "other services" may fail:
Is it normal and expected behavior that missing Q_OBJECT macro is not detected at compile or link time, but corrupts the runtime?
Yes - how should e.g. the meta object system know something about if a custom class has a Q_OBJECT macro or not?
All this won't work: https://doc.qt.io/qt-6/qmetaobject.html and also see https://doc.qt.io/qt-6/qobject.html#qobject_cast which is a common usecase for a QObject derived class (but it will fail during compile time when the class to cast to has no Q_OBJECT)
/edit: and when there is a segfault you should use a debugger to see where it crashes.
Thanks. This is an excellent hint. There are indeed some
qobject_cast
in our code, in close vicinity to the classes that did not sufferQ_OBJECT
to be removed.