Please nominate your Qt Champions for 2021!

Crash with QItemSelectionModel::selectedIndexes()

  • I have read about this problem all over the internet, but always with different solutions and none worked for me.

    When I run the following code:

    @QModelIndexList il = placeList->selectionModel()->selectedIndexes(); //placeList is a QListView@

    a crash occurs. But the crash does not happen when executing this line of code. It happens when the program is destructing the QList object, in @Q_INLINE_TEMPLATE void QList<T>::node_destruct(Node *from, Node *to) @ .
    This does not happen because the list is invalid or something like that. When I check it, the size of the list is exactly the size of the selection and the indexes are the right ones. This message appears when the program crashes:

    bq. Debug Assertion Failed!
    File: f:\dd\vctools\crt_bld\self_x86\crt\src\dbgheap.c
    Expression: _pFirstBlock == pHead

    When running the Release build, the program crashes too, but without this message, obviously.

    Some people say this problem only happens when building Debug with Release libraries, other say this only happens in Release, and everybody I see having this problem is using Qt4.7 or < ... I'm using 5.0 and the crash occurs in Debug and Release builds, using debug libraries in Debug and release libraries in Release.


    I'm adding here the simplest possible code that will cause this problem, so you can analyse and test it. Is just a main.cpp file:

    @#include <QApplication>
    #include <QStringList>
    #include <QStringListModel>
    #include <QItemSelectionModel>
    #include <QModelIndex>
    #include <QModelIndexList>
    #include <QListView>
    #include <QItemSelectionModel>

    void doSomethingWithSelection(QItemSelectionModel* selectionmodel);

    int main(int argc, char *argv[])
    QApplication a(argc, argv);

    QStringList list;
    QStringListModel model(list);
    QListView view;
    QItemSelectionModel *selectionmodel = view.selectionModel();
    QModelIndex first = model.index(0);
    QModelIndex last = model.index(2);
    QItemSelection selection(first, last);
    return a.exec&#40;&#41;;


    void doSomethingWithSelection(QItemSelectionModel* selectionmodel)
    QModelIndexList indexlist = selectionmodel->selectedIndexes();
    // this is what causes the error. I put this inside a function
    // so the error will happen when exiting the function,
    // when the program try to destroy the list nodes.

  • Hi folks. I'm answering myself here with quite a non-conclusive answer, but I managed to solve the problem. Right now I don't understande why this works, yet:

    In my Qt project, I was using a Kit with Microsoft Windows SDK for Windows 7 (7.0.7600.16385.40715) (x86) as compiler. When I changed the compiler to Microsoft Visual C++ Compiler 10.0 (x86) this code worked correctly.

    I really don't know what this means.. as far as I know Windows SDK is an SDK, not a compiler. Can someone please explain to me this situation?

  • Lifetime Qt Champion

    From the top of my head, compiler mismatch between your code and the Qt libraries

Log in to reply