what(): std::bad_alloc Issue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
QVector<task> repository::read()
{
    QFile data("tasks.txt");
    data.open(QFile::ReadOnly | QFile::Text);
    QTextStream reader(&data);

    QVector<task> tlist;
    int id,hours;
    QString name;

    while (!reader.atEnd())
    {
        task t;
        id = reader.readLine().toInt();
        name = reader.readLine();
        hours = reader.readLine().toInt();
        t.tcreate(id,name,hours);
        tlist.append(t);
    }
    return tlist;
}


This is the code for the function with the issue. It compiles but crashes my executed program with exit code 3 and this

terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc

task is a class foe which i have the create function which i use with int,string and int parameters. I do not understand what the issue might be so if anyone knows please help me with a bit of explanation.
PS: I am using QT as some of you might have figured. If you didn't, well, every Q object behaves like the C++ one and the issue is totally linked to the vector that it's supposed to be returned.
std::bad_alloc simply means your program ran out of memory. Perhaps your reader is never atEnd() (e.g. is in an error state: if QT is anything like stdlib), and the loop runs endlessly, appending empty t's to tlist?
Topic archived. No new replies allowed.