Search code examples
c++qtexceptionmemoryqlist

C++ Qt memory allocation exception with QList


How can this ever happen that this throws an exception

    for(int h = 0 ; h < listOne.count() ; ++h) {
        delete[] listOne[h];
    }

with QList listOne ? I delete float* arrays iterating until I reach number of element in the QList ...

EDIT & SOLUTION

In fact, it fails when I am adding only one float in one item of QList. Then, it is no more a float* and you cannot delete [] it.


Solution

  • How can this ever happen that this throws an exception
    

    One posibility: you added one array 2 time to the list. One fix:

    for(int h = 0 ; h < listOne.count() ; ++h) {
        delete[] listOne[h];
        listOne[h]=nullptr;
    }
    

    Maybe others errors (you added not-array pointers to the list).

    EDIT:

    In fact, it fails when I am adding only one float in one item of QList. Then, it is no more a float* and you cannot delete [] it.

    I suspected... A very simple fix:

    float *pi=new float[1];
    pi[0]=3.14f;
    

    Now add pi to the list