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



  • 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
    @



  • 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.



  • 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.



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.