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

Objective-C usage can result in undefined behavior

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P1: Critical P1: Critical
    • 6.2.5, 6.2, 6.3, 6.4
    • 6.2.2
    • GUI: Painting
    • None
    • macOS
    • 89e26cb467 (qt/tqtc-qtbase/6.2)

      Locate "qtbase/src/gui/painting/qcoregraphics_p.h" and you will find an objective-c category for Apples NSImage

      // -------------------------------------
      @interface NSImage (QtExtras)
      + (instancetype)imageFromQImage:(const QT_PREPEND_NAMESPACE(QImage) &)image;
      // -------------------------------------

      Now imagine you have two plug-ins compiled with Qt. Let's say with plug_a with Qt 5.15 and plug_b Qt 6.2. When plug_b with Qt 6.2 itself calls ...
      [NSImage imageFromQImage:myImage];
      ... it is not clear which Qt library (5.15 or 6.2) will be used. 

      Apple warns about those problems
      https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html

      ---> "If the name of a method declared in a category is the same as a method in the original class, or a method in another category on the same class (or even a superclass), the behavior is undefined as to which method implementation is used at runtime. "

      We have some crashes with that current situations. At the moment we use the following workaround which is based on namespaces.

      // -------------------------------------
      #define QtExtras QT_MANGLE_NAMESPACE(QtExtras)
      #define imageFromQImage QT_MANGLE_NAMESPACE(imageFromQImage)
      #define imageFromQIcon QT_MANGLE_NAMESPACE(imageFromQIcon)

      @interface NSImage (QtExtras)
      + (instancetype)QT_MANGLE_NAMESPACE (imageFromQImage):(const QT_PREPEND_NAMESPACE(QImage) &)image;
      + (instancetype)imageFromQIcon:(const QT_PREPEND_NAMESPACE(QIcon) &)icon;
      + (instancetype)imageFromQIcon:(const QT_PREPEND_NAMESPACE(QIcon) &)icon withSize:(int)size;
      // -------------------------------------

      FYI : vestbo

       

       

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

            vestbo Tor Arne Vestbø
            michaspork Michael Spork
            Votes:
            3 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes