Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-38876

QObject::metaObject() returns invalid QMetaObject*

    XMLWordPrintable

Details

    • Bug
    • Resolution: Out of scope
    • Not Evaluated
    • None
    • 5.2.1
    • Core: Object Model
    • None
    • Win7 x64, Qt5.2.1, MSVC2012

    Description

      This code will crash with a null ptr access.

      test.h

      #ifndef TEST_H
      #define TEST_H
      
      #include <QObject>
      
      class Test : public QObject
      {
      	Q_OBJECT
      public:
      	explicit Test(QObject *parent = 0);
      
      signals:
      
      public slots:
      	void testSlot();
      
      };
      
      extern Test issue;
      
      #endif // TEST_H
      

      test.cpp

      #include "test.h"
      
      #include <QMetaMethod>
      
      Test issue;
      
      Test::Test(QObject *parent) :
          QObject(parent)
      {
      	const QMetaObject* object = metaObject();
      	QByteArray normalizedSignature = QMetaObject::normalizedSignature( "testSlot()" );
      	int         methodIndex = object->indexOfMethod( normalizedSignature );
      	QMetaMethod method      = object->method( methodIndex );
      }
      
      void Test::testSlot()
      {
      }
      

      main.cpp

      #include "test.h"
      
      int main(int argc, char *argv[])
      {
      	return 0;
      }
      

      If this is not fixed immediately, at least it should be documented with metaObject() that it may not be used under the conditions as shown above.

      Also, instead of just crashing, a valid error message should be displayed.

      Additionally, there should be a note here about the issue:
      http://qt-project.org/doc/qt-5/qmetamethod.html#invoke
      (I basically copied the crashing code from that place of the documentation.)

      Attachments

        1. qtbug38876.zip
          3 kB
        2. test.7z
          3 kB

        Issue Links

          For Gerrit Dashboard: QTBUG-38876
          # Subject Branch Project Status CR V

          Activity

            People

              thiago Thiago Macieira
              nweyand NWeyand
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes