简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM