QPieSeries::slices() strange behavior



  • 
        QPieSeries* series= new QPieSeries;
        QList<QPieSlice* > sliceList;
    
        sliceList.append(new QPieSlice("una",5));
        sliceList.append(new QPieSlice("doua",20));
    
        series->append(sliceList);
    
        QList<QPieSlice*>::iterator it1 = series->slices().end() -1;
        QList<QPieSlice*>::iterator it2 =        sliceList.end() -1;
    
        qDebug()<<*it1;
        qDebug()<<*it2;
    
    

    qDebug()<<*it1; line crashes the program. Shouldn't series->slices() contain the same pointers to QPieSlice as sliceList does? I can't understand what is wrong in the code. Any ideas ?


  • Qt Champions 2016

    @cpper said in QPieSeries::slices() strange behavior:

    Shouldn't series->slices() contain the same pointers to QPieSlice as sliceList does? I can't understand what is wrong in the code. Any ideas ?

    You're taking an iterator (a pointer) to a temporary. When the temporary is destroyed the iterator becomes invalid and when you call the dereference operator on it, you get a crash.



  • Oops...I understand what you mean. If slices() had returned a reference to the list of the slices my code made sense. I think.
    Thanks :)


  • Qt Champions 2016

    @cpper said in QPieSeries::slices() strange behavior:

    If slices() had returned a reference to the list of the slices my code made sense.

    If it did it would, yes. Now your iterator is pointing to a piece of memory that's been freed.


Log in to reply
 

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