QAbstractItemModel::beginInsertRows() crashes with last == -1 only on Windows



  • Hello,

    I had a bug in my code that I only found out when I built my project for Windows. In:

    @beginInsertRows(index(0,0), 0, devices.count()-1);@

    When devices was empty, the 'last' argument of QAbstractItemModel::beginInsertRows() would be -1. On Windows, this gives a crash with:

    @ASSERT: "last >= first" in file itemmodels\qabstractitemmodel.cpp, line 2542
    Invalid parameter passed to C runtime function.
    Invalid parameter passed to C runtime function.@

    I can understand that the 'invalid parameter passed' error only shows up on Windows, because it may be the Windows API saying it, but why doesn't that ASSERT fail on Linux?

    Qt 5.1.1.



  • could it be, that devices.count() = 0 on Windows?



  • Both on Linux and Windows it's 0. The point is, that on Linux there is no ASSERT crash.



  • I think a good method is not to ask why the fault behavior is different on 2 systems, i prefere to ask myself in such sitations, why haven't i seen, that I have forgotten to catch a fault-situation.



  • If I look at the "source":https://qt.gitorious.org/qt/qt/source/acc9bc4d9dd3b7a1fef10fe88bfa753e515cdd3e:src/corelib/kernel/qabstractitemmodel.cpp#L2137 of beginInsertRows:

    @void QAbstractItemModel::beginInsertRows(const QModelIndex &parent, int first, int last)
    {
    Q_ASSERT(first >= 0);
    Q_ASSERT(last >= first);
    Q_D(QAbstractItemModel);
    d->changes.push(QAbstractItemModelPrivate::Change(parent, first, last));
    emit rowsAboutToBeInserted(parent, first, last);
    d->rowsAboutToBeInserted(parent, first, last);
    }@

    And of Q_ASSERT:

    @#if !defined(Q_ASSERT)

    if defined(QT_NO_DEBUG) && !defined(QT_FORCE_ASSERTS)

    define Q_ASSERT(cond) qt_noop()

    else

    define Q_ASSERT(cond) ((!(cond)) ? qt_assert(#cond,FILE,LINE) : qt_noop())

    endif

    #endif@

    I really don't get it. Both were debug builds. The assert should have failed on Linux too.


Log in to reply
 

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