Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED] Crash of app when exchanging objects from C++ to QML
QtWS25 Last Chance

[SOLVED] Crash of app when exchanging objects from C++ to QML

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 2.2k 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.
  • GianlucaG Offline
    GianlucaG Offline
    Gianluca
    wrote on last edited by
    #1

    Dear all,
    I'm using Qt 5.2.1 for developing an app on iOS.
    In the app there is a C++ part that provides various objects (registered with qmlRegisterType) to the QML part where the data of such objects are displayed.

    I'm experiencing a weird crash of the app during such object exchanging that I don't know how to debug and to solve.

    On C++, I have some slot functions that are used on QML:
    @
    // return the list of object id to use in getTrait slot below
    QStringList DatObject::getTraits() {
    return orderedTraits;
    }
    // return an object that it is used on QML part
    TraitObject *DatObject::getTrait( QString traitId ) {
    if ( traits.contains(traitId) ) {
    qDebug() << "GetTrait:" << traits[traitId] << " - " << traitId;
    return traits[traitId];
    }
    // return a dummy created object (this should never happens)
    // ---- This is a leaky but it will be not called
    return (new TraitObject());
    }
    @

    on QML, I have a ListView for display a list of objects:
    @
    ListView {
    id: listTraits
    model: personalDat.dat.getTraits()
    delegate: TraitView {
    width: parent.width
    Component.onCompleted: {
    var trait = personalDat.dat.getTrait( modelData )
    console.log( "TraitView: "+trait+" - "+modelData )
    if ( trait != null ) {
    traitObject = trait
    isEditable = personalDat.dat.isMe()
    }
    }
    }
    }
    @

    Into the slot getTrait and into the delegate TraitView, I put some print out to show what happens.
    The QML file whit the ListView is pushed and popped from a StackView various time for showing different list of TraitObjects.
    The code works well for a while (not always the same number of time), and a certain time it crash !!
    The debug prints show something weird:
    @
    QML Side -> DAT to view: DatObject(0x17d06f20)
    C++ Side -> GetTrait: TraitObject(0x17f92340) - "5367cdf7698b3c4d46010098"
    QML Side -> TraitView: TraitObject(0x17f92340) - 5367cdf7698b3c4d46010098
    C++ Side -> GetTrait: TraitObject(0x17f8f770) - "5367cdf8698b3c0a5a033401"
    QML Side -> TraitView: TraitObject(0x17f8f770) - 5367cdf8698b3c0a5a033401
    C++ Side -> GetTrait: TraitObject(0x17f8f1c0) - "5367ce0b698b3c4d460100a8"
    QML Side -> TraitView: null - 5367ce0b698b3c4d460100a8
    @

    If you note the last two rows, the debug print into the getTraits in C++ indicate that a valid TraitObject is going to be returned to the QML ... but the debug print on the QML indicate that it's arrived a "null" object !!
    Why ?!?!
    And as soon as this happens the app crash with a lot of messages like:
    @
    program received signal -111, <something-very-long-that-I-dont-understand>
    hit maximum number of signal, stopping
    @

    1 Reply Last reply
    0
    • T Offline
      T Offline
      Torgeir
      wrote on last edited by
      #2

      My first guess would be that this crash is caused by Qml assuming ownership of the object returned from getTrait()

      Please see "Data ownership":http://qt-project.org/doc/qt-5/qtqml-cppintegration-data.html

      This is also why the comment in:

      @
      // ---- This is a leaky but it will be not called
      return (new TraitObject());
      @

      is incorrect. This will not leak because Qml assumes ownership and disposes the object when it is no longer referenced.

      1 Reply Last reply
      0
      • GianlucaG Offline
        GianlucaG Offline
        Gianluca
        wrote on last edited by
        #3

        Ohhh my god !!
        Your guess is correct. The QML take ownership and destroy my objects ... while instead the ownership has to be on the C++.
        I'll fix it, and I'll try.
        Thanks.

        1 Reply Last reply
        0
        • GianlucaG Offline
          GianlucaG Offline
          Gianluca
          wrote on last edited by
          #4

          I solved setting the parent of TraitObject to the C++ object that creates and maintains it.
          In this way, the QML does not take ownership because it see that has a parent and it suppose that the parent manages the object.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved