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

Unpredictable behavior of QMetaMethod::invoke

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Not Evaluated
    • Resolution: Invalid
    • Affects Version/s: 5.15.2, 6.3.0
    • Fix Version/s: None
    • Component/s: Core: Object Model
    • Labels:
      None
    • Environment:
      Clang 11, GCC 10.2.1, MinGW 8.1
    • Platform/s:
      Linux/X11, Windows

      Description

      Calling QMetaMethod::invoke of one object on different object causes unexpected (yet slightly controllable) behavior. There is no validation.

      Consider the following example:

      firstclass.h

      #ifndef FIRSTCLASS_H
      #define FIRSTCLASS_H
      
      #include <QObject>
      #include <QDebug>
      
      class FirstClass: public QObject {
          Q_OBJECT
      public:
          int firstDigit = 1;
          Q_INVOKABLE void aMethod() {qInfo() << "First" << firstDigit;}
      };
      #endif // FIRSTCLASS_H
      

      secondclass.h

      #ifndef SECONDCLASS_H
      #define SECONDCLASS_H
      
      #include <QObject>
      
      class SecondClass: public QObject {
          Q_OBJECT
      public:
          int secondDigit = 2;
      };
      #endif // SECONDCLASS_H
      

      main.cpp

      #include <QCoreApplication>
      #include <QMetaMethod>
      #include "firstclass.h"
      #include "secondclass.h"
      
      int main(int argc, char *argv[]) {
          QCoreApplication a(argc, argv);
          FirstClass first;
          SecondClass second;
          auto firstIndex = first.metaObject()->indexOfMethod("aMethod()");
          auto firstMethod = first.metaObject()->method(firstIndex);
      
          firstMethod.invoke(&first, Qt::DirectConnection); // Returns True
          // Expected output: "First 1"                    | Actual output "First 1"
      
          firstMethod.invoke(&second, Qt::DirectConnection); // Also returns True
          // Expected output: No output? Error? "First 1"? | Actual output "First 2"
      
          return 0;
      }
      

       

       I would expect for invoke to at least verify that such a method (aMethod()) exists in the second object and return false or at least give me a runtime warning. Yet it silently uses FirstClass method with SecondClass data. You can even replace second object with empty QObject and get a random number from memory in the output. 

        Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

            Assignee:
            thiago Thiago Macieira
            Reporter:
            mfoxru Mikhail Kravchik
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:

                Gerrit Reviews

                There are no open Gerrit changes