-1

In My class I have a member variable;

QProcess* p1;

Inside some function, i initialize and use it as:

process1 = new QProcess();

it works fine. Now i have a situation where i have many of these processes to be started. One option is to declare all of them as member functions:

QProcess* p1;
QProcess* p2;
QProcess* p3;
...

And then initialize all of them when needed. However this is too much redundant work. So i tried to create a list and initialize it in a loop like this:

QList<QProcess*> procList;

for(int i=0; i<len; i++){
   procList[i] = new QProcess();
}

It compiles fine but then crashes. Is there something missing or what am i doing wrong here?

I also tried to add all member variables in this list like:

   for(int i=0; i<len; i++){
       switch(i){
          case 0:
             procList[i] = p1;
             break;
       }

    }

But this also has the same result like above

EDIT:

Based on your suggestion, i tried:

procList.append(new QProcess());

as well as procList.append(p1);

But result is same, it compiles but crashes at run time

EDIT: So i found the issue was totally unrelated. The class itself where i was using this code (custom class i made) had no default constructor. And as i learned without default constructor, if you initialize a pointer, somehow it just crashes.... strange. After specifying default constructor it is working fine now.

  • I would suggest using `std::vector procList;` rather than `QList procList;`. You cannot get much more efficient than a vector (in most cases), but you can really blow performance with a list. – Jesper Juhl Jul 22 '19 at 16:29
  • Is `QList` sized appropriately to hold `len` elements? I am assuming that `QList` is a sequence container that relies on those elements addressed by `[ ]` to already exist, unlike associative containers like `std::map` and `std::unordered_map`, where `[ ]` actually inserts a item if the key doesn't exist. – PaulMcKenzie Jul 22 '19 at 16:29
  • @JesperJuhl i get the same results. Putting any primitive data type works just fine. – johny bravo Jul 22 '19 at 16:33
  • @johnybravo I didn't say it would fix your current problem. I just wanted to tell you that lists are, in most cases, terribly performing data structures. – Jesper Juhl Jul 22 '19 at 16:34

1 Answers1

1

You are accessing areas of the memory that is not allocated. Your list is empty and you access one element that is not valid. As per documentation: https://doc.qt.io/qt-5/qlist.html#operator-5b-5d

T &QList::operator[](int i) Returns the item at index position i as a modifiable reference. i must be a valid index position in the list (i.e., 0 <= i < size()).

If this function is called on a list that is currently being shared, it will trigger a copy of all elements. Otherwise, this function runs in constant time. If you do not want to modify the list you should use QList::at().

Try to use append or push_back, see https://doc.qt.io/qt-5/qlist.html#push_back

PS: Not an expert of QT, but you might want to look into a QList of std::unique_ptr to manage your memory if possible. Otherwise you risk to forget to delete the heap-allocated elements whose pointer is stored in the list.

Edit: OP reports that the suggestion did not work. I wrote a small example myself (disregarding possible leaks). The following example crashes when using operator[] but works with append in debug mode (I used QT creator for Windows with Qt 5.13.0 for MinGW 64 bits) . What OP is experiencing can be either some issue with the toolchain or some undefined behavior triggered before the append. I suggest OP tries to copy/paste my code in a clean project and run it.

#include <QList>
#include <QProcess>

int main()
{
    QList<QProcess*> list;

    for(int i = 0; i < 10; ++i){
        QProcess * p = new QProcess();

        //Decomment and crash
        //list[i] = p;

        //does not crash
        list.append(p);
    }
    //Here you should cleanup
}
  • calling procList.append(new QProcess()) has the same effect. It compiles but crashes at run time. – johny bravo Jul 22 '19 at 16:27
  • 1
    @johnybravo what are you doing after populating the list? And more importantly, can you confirm that the crash happens at the same spot? – CuriouslyRecurringThoughts Jul 22 '19 at 16:36
  • Im not able to do anything, as the moment i put a QProcess pointer in the list of type QProcess*, the debugger shows crash happened at the append function. Similarly if i directly create the object at the append function, same result – johny bravo Jul 23 '19 at 07:27