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. Multiple asynchronous QProcesses
Qt 6.11 is out! See what's new in the release blog

Multiple asynchronous QProcesses

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 949 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.
  • E Offline
    E Offline
    E106JZ
    wrote on last edited by E106JZ
    #1

    I'm trying to run multiple QProcess at runtime (around 1-12) aynchronously so I create a vector of unique_ptr for each QProcess and connect them in a loop like this:

    std::vector<std::unique_ptr<QProcess>> processes;
    processes.resize(threads);
    for (int i=0; i<threads; i++)
    {
        processes[i] = std::make_unique<QProcess>();
    }
    ...
    for (int i=0; i<threads; i++)
    {
    	connect(processes[i].get(), &QProcess::readyReadStandardOutput, this, [&]()
    	{
    		QByteArray readStdOut = processes[i]->readAllStandardOutput();
    	});
    }
    

    However when I actually start my processes and receive data, I always get a segmentation fault because the index i is always equal to threads when data is received. Why does this happen and how do I fix this?

    Thanks

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      You capture i by reference so it will always point to the last values and not the value it had at that moment.

      
      Try
      ....[&processes, i ](){};
      or
      ....[&, i ](){};
      

      so we capture process by ref and then i by value.

      1 Reply Last reply
      7
      • E Offline
        E Offline
        E106JZ
        wrote on last edited by
        #3

        Thanks :)

        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