Q:

Replacing vector with array or static vector for a better approach

My application runs particular number of processes(different executables) using createprocess(windows api) in parallel(using threads). User can refresh / close my application at any time. As of now, I am pushing the process handles into vector and whenever close request received, I am iterating the vector and terminating(using GetExitCodeProcess and TerminateProcess APIs) and closing(using CloseHandle API) the process handles. Also I am closing the handle of the process when it is completed. The problem with current model is, whenever process completed handle will be closed and when close request received again I will try to close it using vector(handle is not updated). To solve this, I have to update/remove the handle in/from the vector. To do this, need to maintain index.
Since I know the number of process, I want to create a static vector and update it rather than pushing a local object to a vector. Can someone suggest a best approach.
Below is the sample code.
//member object
std::vector<PROCESS_INFORMATION> mProcessHandles;

//this is a thread and will be called multiple times with different executable names in the application
void method(std::string executable)
{
    STARTUPINFO startInfo{};
    PROCESS_INFORMATION procInfo{};
    bool ret  = CreateProcess(NULL, executable, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &startInfo, &procInfo);
    mProcessInfo.push_back(procInfo);
    if(ret)
    {
        WaitForSingleObject(procInfo.hProcess, INFINITE);
        CloseHandle(procInfo.hProcess);
        procInfo.hProcess = NULL;
        CloseHandle(procInfo.hThread);
        procInfo.hThread = NULL;
    }
    return;
}

//this will be called when application close requested
void forceKill()
{
    for (auto &processHandlesIt : mProcessHandles)
    {
        DWORD errorcode = 0;
        GetExitCodeProcess(processHandlesIt.hProcess, &errorcode);
        if (errorcode == STILL_ACTIVE)
        {
            TerminateProcess(processHandlesIt.hProcess, errorcode);
        }
        CloseHandle(processHandlesIt.hProcess);
        processHandlesIt.hProcess = NULL;
        CloseHandle(processHandlesIt.hThread);
        processHandlesIt.hThread = NULL;
    }
}

A:

You should not use handles (in GetExitCodeProcess for example) after they are closed.
I would simply not close those process handles in the threads, and just leave them for the forceKill or other clean-up function to close.
Also, since you are not using procInfo.hThread, you could close it right after CreateProcess returns.
I guess you are not using any other members of the procInfo, so you could only store the process' handles in your vector.

