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. ASSERT failure in QList<T>::at: "index out of range"
QtWS25 Last Chance

ASSERT failure in QList<T>::at: "index out of range"

Scheduled Pinned Locked Moved Solved General and Desktop
qlistqstringlistc++qurlqt 5.7
24 Posts 4 Posters 25.3k Views
  • 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.
  • Q Offline
    Q Offline
    Qjay
    wrote on 24 Jul 2016, 18:19 last edited by
    #1

    Hey all i am getting this error

    ASSERT failure in QList<T>::at: "index out of range", file ../../Qt5.7.0/5.7/gcc_64/include/QtCore/qlist.h, line 537
    

    So i am trying to download images from a List of links ( StringList )

    Screenshot of what is happening

    https://s31.postimg.org/8l3unwe1n/assert.gif

    below is my code snippet ( i have not pasted whole code it's too big )

    Code snippet :

     dbmanager *d = new dbmanager(0) ;
        d->doDownload(down_links);
    
        dbmanager *p = new dbmanager(0) ;
        p->png_download(png_down_links, png_hash);
    
        return true ;
    
    }
    
    
    //start downloading images
    
    
    
    
    void dbmanager::doDownload(const QVariant& v)
    {
        if (v.type() == QVariant::StringList) {
    
    
            QNetworkAccessManager *manager= new QNetworkAccessManager(this);
    
            QUrl url = v.toStringList().at(current);
            qDebug() << "why break" << url;
    
            filename = url.toString().remove("http://restbase.wikitolearn.org/en.wikitolearn.org/v1/media/math/render/svg/");
            qDebug() << filename;
            m_network_reply = manager->get(QNetworkRequest(QUrl(url)));
    
            connect(m_network_reply, SIGNAL(downloadProgress (qint64, qint64)),this, SLOT(updateDownloadProgress(qint64, qint64)));
            connect(m_network_reply,SIGNAL(finished()),this,SLOT(downloadFinished()));
    
    
        }
    }
    
    
    
    void dbmanager::downloadFinished(){
        qDebug()<<filename;
        if(m_network_reply->error() == QNetworkReply::NoError){
    
    
            m_file =  new QFile(imgpath+"/"+filename+".svg");
            qDebug()<<imgpath+"/"+filename;
            if(!m_file->open(QIODevice::ReadWrite | QIODevice::Truncate)){
                qDebug() << m_file->errorString();
            }
            m_file->write(m_network_reply->readAll());
    
            QByteArray bytes = m_network_reply->readAll();
            QString str = QString::fromUtf8(bytes.data(), bytes.size());
            int statusCode = m_network_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
            qDebug() << QVariant(statusCode).toString();
        }
        m_file->flush();
        m_file->close();
        int total = down_links.count();
        if(current<total-1){
            current++;
            qDebug()<<"current = "<<current<<"total = "<<total;
            doDownload(down_links);}
        else if(current==total-1)
        {
            qDebug()<<"download complete";
        }
    
        bool success = false ;
        QString fname = filename;
        success = add_depend(fname,revision_number);
        if(success == true)
        {
            qDebug() <<"added in dependency table";
        }
        else
        {
            qDebug() << " error in adding to dependency table";
        }
    
    
    }
    
    
    void dbmanager::updateDownloadProgress(qint64 bytesRead, qint64 totalBytes)
    {
        //    ui->progressBar->setMaximum(totalBytes);
        //    ui->progressBar->setValue(bytesRead);
        qDebug()<<bytesRead<<totalBytes;
    }
    
    

    Explanation :

    down_links : contains list of download links . These contatin links i am sure , i used qDebug() << down_links to check them

    the problem that i was able to identify till now is this :

     QUrl url = v.toStringList().at(current);
            qDebug() << "why break" << url;
    

    in above code url should contatin download link but it does not , it is empty .

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 24 Jul 2016, 20:25 last edited by
      #2

      Hi,

      Did you check that you call doDownload with an non-empty QStringList ?

      Also, why use QVariant if you will only handle string list in that method ?

      On a side note, you have a memory leak, each time that function is called you create a new QNetworkAccessManager but never delete it. All instances will get destroyed when their parent also is but in between they will slowly fill your memory.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        Qjay
        wrote on 25 Jul 2016, 03:56 last edited by
        #3

        Yes i did checked that the doen_links (string list) is not empty.

        I did qDebug() << down_links.size();

        To check the size of the list.

        For instance in the givrn screenshot the page that i tried to download had 33 links

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 25 Jul 2016, 07:15 last edited by
          #4

          Did you also check the value of current ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          Q 1 Reply Last reply 25 Jul 2016, 14:14
          2
          • Paul ColbyP Offline
            Paul ColbyP Offline
            Paul Colby
            wrote on 25 Jul 2016, 07:18 last edited by
            #5

            Hi @Qjay

            Where do you initialise dbmanager::current? And, are you re-using the dbmanager instance?

            eg:

            d->doDownload(one_set_of_down_links);
            // later
            d->doDownload(another_set_of_down_links);
            

            In which case you might need to be explicitly resetting dbmanager::current to 0? (and if the second list was smaller than the first, you will get the exception you've shown)

            Cheers.

            1 Reply Last reply
            3
            • Q Offline
              Q Offline
              Qjay
              wrote on 25 Jul 2016, 11:39 last edited by
              #6

              hi @Paul like in my case

               dbmanager *d = new dbmanager(0) ;
                  d->doDownload(down_links);
              
                  dbmanager *p = new dbmanager(0) ;
                  p->png_download(png_down_links, png_hash);
              

              i use
              int current= 0 // for doDownload(down_links)
              png_curr = 0; // for png_download

              here is the whole code of the .cpp file

              https://ghostbin.com/paste/rdfnb

              1 Reply Last reply
              0
              • S SGaist
                25 Jul 2016, 07:15

                Did you also check the value of current ?

                Q Offline
                Q Offline
                Qjay
                wrote on 25 Jul 2016, 14:14 last edited by
                #7

                @SGaist

                Yes i check the value of current .

                it is in global scope and i have initialized it to 0

                int current = 0 ;

                i have attached the full source code too

                https://ghostbin.com/paste/rdfnb

                and this is how i am calling the add function which calls save image and save file function

                   Item{
                                visible: false
                                Text{
                                    visible: false
                                    id: responsetext
                                    text:""
                                }
                            }
                
                            Button{
                                width: drawer.width
                                height: 40
                
                                text:"save offline"
                                onClicked: {
                                    responsetext.text = dbman.add(current_title);
                                    drawer.close();
                
                                }
                            }
                
                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 25 Jul 2016, 20:47 last edited by
                  #8

                  Why a global variable ?

                  If you have an out of range error this means that current goes higher than the size of the list your are accessing.

                  You are likely never reinitializing current. So it will work the first time however the second time, and unless your array gets bigger than current, it will fail.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • Paul ColbyP Offline
                    Paul ColbyP Offline
                    Paul Colby
                    wrote on 25 Jul 2016, 22:21 last edited by
                    #9

                    @Qjay, how sure are you that the exception happens at this line?

                    QUrl url = v.toStringList().at(current);
                    

                    Did the debugger break at this line?

                    The reasons I ask are,

                    1. the output in your animated gif suggests (its not definitive) to me that the crash happens just after that point;
                    2. its looks very likely to me, that the exception would be on this line:
                    png_filename = n.toStringList().at(png_curr);
                    

                    Because the code in dbmanager::png_download is assuming that n (aka png_hash) always has the same number (or rather, at least as many) items as v (aka png_down_links). But, while I see code that streams entries into png_down_links (in save_images), I see no code anywhere that adds anything to png_hash.

                    I highly recommend you try out the debugger (if you haven't already), and also add some range-checking / debug logging in the dbmanager::png_download function.

                    Cheers.

                    1 Reply Last reply
                    1
                    • Q Offline
                      Q Offline
                      Qjay
                      wrote on 26 Jul 2016, 02:12 last edited by Qjay
                      #10

                      @Paul i think you are right . I think my code is messed up ( this code is an older version ) . Yeah i just saw png_hash has nothing to do in whole code !!

                      I have updated the code

                      https://ghostbin.com/paste/j4meh

                      1 Reply Last reply
                      0
                      • Q Offline
                        Q Offline
                        Qjay
                        wrote on 26 Jul 2016, 12:13 last edited by
                        #11

                        @Paul @SGaist , i don't know why but i am never able to initialize QUrl url :( ;

                        AND I BELIEVE THAT"S WHAT CAUSING THE PROBLEM

                        i have tried different approaches too . Like in my code i was using QStringList i even tried Qvector<QString> down_links and end result was same

                        NOT ABLE TO set QUrl

                        in console screen
                        QUrl("")
                        ""
                        

                        What shoudl i do ?

                        jsulmJ 1 Reply Last reply 26 Jul 2016, 12:33
                        0
                        • Q Qjay
                          26 Jul 2016, 12:13

                          @Paul @SGaist , i don't know why but i am never able to initialize QUrl url :( ;

                          AND I BELIEVE THAT"S WHAT CAUSING THE PROBLEM

                          i have tried different approaches too . Like in my code i was using QStringList i even tried Qvector<QString> down_links and end result was same

                          NOT ABLE TO set QUrl

                          in console screen
                          QUrl("")
                          ""
                          

                          What shoudl i do ?

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 26 Jul 2016, 12:33 last edited by
                          #12

                          @Qjay Can you show the code where you initialize QUrl? From your description we can only guess what could be the problem. Are you sure you don't pass an empty string to QUrl?

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • Q Offline
                            Q Offline
                            Qjay
                            wrote on 26 Jul 2016, 16:10 last edited by
                            #13

                            @jsulm , i have given the whole code

                            https://ghostbin.com/paste/v4t47

                            by the way this is where i set QUrl

                            line no 371

                             QUrl url = v.toStringList().at(current);
                                    qDebug() << "why break" << url;
                            
                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on 26 Jul 2016, 21:38 last edited by
                              #14

                              Again, did you check that the value of current is lower than the size of the string list ?

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0
                              • Q Offline
                                Q Offline
                                Qjay
                                wrote on 27 Jul 2016, 03:37 last edited by
                                #15

                                I have initialized current =0; in global scope .

                                jsulmJ 1 Reply Last reply 27 Jul 2016, 04:15
                                0
                                • Q Qjay
                                  27 Jul 2016, 03:37

                                  I have initialized current =0; in global scope .

                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on 27 Jul 2016, 04:15 last edited by
                                  #16

                                  @Qjay But is it changed somewhere?
                                  Why not just check like this?

                                  qDebug() << v.toStringList().size() << current;
                                  qDebug() << v.toStringList().at(current);
                                  QUrl url = v.toStringList().at(current);
                                  

                                  In such a situation this is actually the first thing to do...

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  1
                                  • Q Offline
                                    Q Offline
                                    Qjay
                                    wrote on 27 Jul 2016, 08:18 last edited by
                                    #17

                                    hey @jsulm

                                    i tried what you suggested

                                    qDebug() << v.toStringList().size() << current;
                                    qDebug() << v.toStringList().at(current);
                                    QUrl url = v.toStringList().at(current);
                                    

                                    i did this

                                    qDebug() << "stringlist size and current" << v.toStringList().size() << current;
                                            qDebug() << "current url in stringlist" << v.toStringList().at(current);
                                            QUrl url = v.toStringList().at(current);
                                            qDebug() << "why break" << url;
                                    

                                    output

                                    stringlist size and current 33 0
                                    current url in stringlist   "(http://restbase.wikitolearn.org/en.wikitolearn.org/v1/media/math/render/svg/3cd95482da53d42c5f7f249454f7ee1e85cacc0c)"
                                    why break QUrl("")
                                    ""
                                    

                                    and then error

                                    ASSERT failure in QList<T>::at: "index out of range", file ../../Qt5.7.0/5.7/gcc_64/include/QtCore/qlist.h, line 537
                                    The program has unexpectedly finished.
                                    
                                    jsulmJ 1 Reply Last reply 27 Jul 2016, 09:17
                                    0
                                    • Q Qjay
                                      27 Jul 2016, 08:18

                                      hey @jsulm

                                      i tried what you suggested

                                      qDebug() << v.toStringList().size() << current;
                                      qDebug() << v.toStringList().at(current);
                                      QUrl url = v.toStringList().at(current);
                                      

                                      i did this

                                      qDebug() << "stringlist size and current" << v.toStringList().size() << current;
                                              qDebug() << "current url in stringlist" << v.toStringList().at(current);
                                              QUrl url = v.toStringList().at(current);
                                              qDebug() << "why break" << url;
                                      

                                      output

                                      stringlist size and current 33 0
                                      current url in stringlist   "(http://restbase.wikitolearn.org/en.wikitolearn.org/v1/media/math/render/svg/3cd95482da53d42c5f7f249454f7ee1e85cacc0c)"
                                      why break QUrl("")
                                      ""
                                      

                                      and then error

                                      ASSERT failure in QList<T>::at: "index out of range", file ../../Qt5.7.0/5.7/gcc_64/include/QtCore/qlist.h, line 537
                                      The program has unexpectedly finished.
                                      
                                      jsulmJ Offline
                                      jsulmJ Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on 27 Jul 2016, 09:17 last edited by
                                      #18

                                      @Qjay Why is the URL in parentheses?

                                      (http://restbase.wikitolearn.org/en.wikitolearn.org/v1/media/math/render/svg/3cd95482da53d42c5f7f249454f7ee1e85cacc0c)
                                      

                                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      1 Reply Last reply
                                      0
                                      • S Offline
                                        S Offline
                                        SGaist
                                        Lifetime Qt Champion
                                        wrote on 27 Jul 2016, 10:39 last edited by
                                        #19

                                        Looks like you are doing a lot of parsing, replacement etc.

                                        The first thing I'd do, is to just ensure that you really have URLs in your list. And get rid of that QVariant parameter. It really doesn't make any sense in your use case.

                                        Interested in AI ? www.idiap.ch
                                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        1 Reply Last reply
                                        0
                                        • Q Offline
                                          Q Offline
                                          Qjay
                                          wrote on 27 Jul 2016, 15:48 last edited by Qjay
                                          #20

                                          Hello , thanks for the suggetions .

                                          1. yes there were useless ( ) in url . i have removed them . Now i am able to assign url to Qurl :D great!!

                                          2. i removed the Qvariant thing too

                                          now the code is

                                          .cpp : https://ghostbin.com/paste/vv2w7

                                          header file : https://ghostbin.com/paste/8z4y3

                                          but the assert error still exist :/

                                          1 Reply Last reply
                                          0

                                          1/24

                                          24 Jul 2016, 18:19

                                          • Login

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