Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPieSeries::slices() strange behavior

QPieSeries::slices() strange behavior

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 941 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • cpperC Offline
    cpperC Offline
    cpper
    wrote on last edited by cpper
    #1
    
        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 ?

    kshegunovK 1 Reply Last reply
    0
    • cpperC cpper
      
          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 ?

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @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.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      1
      • cpperC Offline
        cpperC Offline
        cpper
        wrote on last edited by
        #3

        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 :)

        kshegunovK 1 Reply Last reply
        0
        • cpperC cpper

          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 :)

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @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.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved