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

  • Lifetime Qt Champion

    Did you also check the value of current ?

  • Hi @Qjay

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


    // later

    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)


  • hi @Paul like in my case

     dbmanager *d = new dbmanager(0) ;
        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

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

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

                    visible: false
                        visible: false
                        id: responsetext
                    width: drawer.width
                    height: 40
                    text:"save offline"
                    onClicked: {
                        responsetext.text = dbman.add(current_title);

  • Lifetime Qt Champion

    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.

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


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

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


    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

    What shoudl i do ?

  • Moderators

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

  • @jsulm , i have given the whole code

    by the way this is where i set QUrl

    line no 371

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

  • Lifetime Qt Champion

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

  • I have initialized current =0; in global scope .

  • Moderators

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

  • 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;


    stringlist size and current 33 0
    current url in stringlist   "("
    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.

  • Moderators

    @Qjay Why is the URL in parentheses?


  • Lifetime Qt Champion

    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.

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

    header file :

    but the assert error still exist :/

  • Lifetime Qt Champion

    Still at the same place ?

  • Hey @SGaist , @jsulm , thank you it's solved now

    To be honest i don't know why it worked :/

    but below is the code

  • Lifetime Qt Champion

    From the looks of it, you're currently lucky.

    I'd recommend considering refactoring and cleaning up this code. Maybe a full rewrite would even be better.

  • I too was also thinking of full rewrite but i don't have much time for that right now :/ .

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