Multithreading conversion of QModelIndexList to treeItem crash



  • Hey

    I'm working on optimizing some of my functions, I recently come across an unusual bug.

    Here is roughly the function:;

    listPrimay = selectedIndexes()/// simplified, this is just a list of column 0 indexes after filtering in larger app, just dropped selecttedIndexes() for simplicity here. 
    #pragma omp parallel for
        for (int x = 0; x < listPrimary.size(); ++x) {
            mNodeListSelected[x] = *static_cast< std::shared_ptr<myTreeNode> * >(listPrimary[x].internalPointer());
        }
    

    Now if I run this app, and I do shift+select, it all goes well. But if I do a lot of quick selections 1 after another. Then I get a crash with following crash debug log :

    testApp.exe!QTypedArrayData<QModelIndex>::deallocate(QArrayData * data) Line 238	C++
    testApp.exe!QVector<QModelIndex>::freeData(QTypedArrayData<QModelIndex> * x) Line 543	C++
    testApp.exe!QVector<QModelIndex>::reallocData(const int asize, const int aalloc, QFlags<enum QArrayData::AllocationOption> options) Line 639	C++
    testApp.exe!QVector<QModelIndex>::detach() Line 391	C++
    testApp.exe!QVector<QModelIndex>::data() Line 127	C++
    testApp.exe!QVector<QModelIndex>::operator[](int i) Line 437	C++
    testApp.exe!myTree::notifyOfNewSelection$omp$1() Line 774	C++
    

    (1st call - being last call)

        static void deallocate(QArrayData *data)
        {
            Q_STATIC_ASSERT(sizeof(QTypedArrayData) == sizeof(QArrayData));
            QArrayData::deallocate(data, sizeof(T), Q_ALIGNOF(AlignmentDummy));
        }
    
    FATAL: ASSERT failure in QArrayData::deallocate: "Static data can not be deleted", file tools\qarraydata.cpp, line 166
     (tools\qarraydata.cpp:166, (null))
    

    What should I do? I'm lost, and I could really use multithreading conversion from 1 type item to another.

    Seems to be like Qt might be invalidating/deleting the indexes between selections while the loop converts them to items and since qt does it in different thread I lose them? Or hmmm ? No idea what to do here. I tried using mutex/static mutext but no luck.

    Regards
    Dariusz
    TIA


  • Moderators

    Don't use multithreading here at all - just using multithreading to do a static case is useless.

    /edit: and your threading problem exists because you're writing to a container from different thread without synchronizing the access with e.g. a mutex



  • @Christian-Ehrlicher Thanks for info.
    "Don't use multithreading here at all - just using multithreading to do a static case is useless."
    Well I have 100k items +/- to deal with. Doing it on 1 core feels somewhat slow I think...

    "/edit: and your threading problem exists because you're writing to a container from different thread without synchronizing the access with e.g. a mutex"
    I created the base vectors via QVector<myTreeNode> vec(vecSize) and I access them all via [], so no thread should step over another thread, There should be no need for mutex here I think?

    Regards
    Dariusz


  • Moderators

    @Dariusz said in Multithreading conversion of QModelIdnexList to treeItem crash:

    Well I have 100k items +/- to deal with

    Then you should avoid to dereference the pointers to not copy the data around.

    I created the base vectors via QVector<myTreeNode> vec(vecSize) and I access them all via [], so no thread should step over another thread,

    It does as your backtrace shows. Since you're using QVector which is implicit shared every write access to the container checks if the container needs to get detached. Maybe try with a non-implicit shared container instead (e.g. a std::vector)
    See http://doc.qt.io/qt-5/implicit-sharing.html for details.



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