dimanche 26 janvier 2020

c++ 11 std::vector courption on reference update

Please read the entire question before commenting, since I have attempted many avenues to solve this issue before posting here.

I am passing a vector by reference to another class method to input values into the vector. This works fine in the recipient function. The values are confirmed and correct. then about using the vector in the sender function, the vector is corrupted.

sender/parent function:

std::vector<std::string> update_jobs;

client_run_job runner = client_run_job();
runner.check_running_jobs(pids_to_check, update_jobs);

std::cout << "size of vector: " << update_jobs.size() << std::endl;
std::cout << "vector at 0: " << update_jobs.at(0) << std::endl;
std::cout << "vector at 1: " << update_jobs.at(1) << std::endl;
std::cout << "vector at 2: " << update_jobs.at(2) << std::endl;
std::cout << "vector at 3: " << update_jobs.at(3) << std::endl;
std::cout << "vector at 4: " << update_jobs.at(4) << std::endl;
std::cout << "vector at 5: " << update_jobs.at(5) << std::endl;
std::cout << "vector at 6: " << update_jobs.at(6) << std::endl;


for(std::vector<std::string>::iterator it = update_jobs.begin(); it != update_jobs.end(); ++it) {
    std::cout << "iterator: " << *it << std::endl;
}

for(auto const& value: update_jobs)
{
    std::cout << "range: " << value << std::endl;
}

recipient function:

std::string client_run_job::check_running_jobs(std::string pids, std::vector<std::string> & codes){
std::vector<std::string> pid_list;
std::cout << "pid list: " << pids << std::endl;

/* split string*/
tokenize(pids,';',pid_list);

std::cout << " count: " << pid_list.size() << std::endl;

for (int i = 0; i < pid_list.size(); i++)
{

    std::string pid = pid_list.at(i);
    std::cout << "############" << std::endl;
    std::cout << "pid str: " << pid << std::endl;
    std::cout << "############" << std::endl;

    std::string running_job_id = pid.substr(0, pid.find(':'));
    std::string job_queue_id = pid.substr(pid.find(',') + 1, pid.size() - pid.find(',') );
    pid = pid.substr(pid.find(':') + 1, pid.find(',') - pid.find(':') - 1);
    std::cout << "running_job_id: " << running_job_id << std::endl;
    std::cout << "job_queue_id: " << job_queue_id << std::endl;
    std::cout << "testing pid: " << pid << std::endl;

    std::string file = "./output/output_" + pid + ".exit";
    std::string temp = running_job_id + ":" + pid+"," + job_queue_id;
    std::cout << "to write: " << temp << std::endl;

    if ( ! is_empty_file( file ) )
    {

        if ( success_exit(file) )
        {
            /* success */
            codes.push_back( temp + ";success");
        }
        else
        {
            /* error */
            codes.push_back( temp + ";error");
        }
    }
    else
    {

        int pid_number = std::stoi(pid);

        if ( pid_number < 0)
        {
            codes.push_back( temp + ";error");
        }
        else if( ! _pid_exist(pid_number) )
        {
            /* Canceled */
            codes.push_back( temp + ";canceled");
        }
    }


    std::cout << "================" << std::endl;
    std::cout << "last entry: " << codes.back() << std::endl;
    std::cout << "================" << std::endl;
}}

Output:

pid list: 2:6049,1;3:6157,1;4:6242,1;5:6855,1;8:7218,1;9:4952,2;10:5098,3
count: 7
############
pid str: 2:6049,1
############
running_job_id: 2
job_queue_id: 1
testing pid: 6049
to write: 2:6049,1
================
last entry: 2:6049,1;canceled
================
############
pid str: 3:6157,1
############
running_job_id: 3
job_queue_id: 1
testing pid: 6157
to write: 3:6157,1
================
last entry: 3:6157,1;canceled
================
############
pid str: 4:6242,1
############
running_job_id: 4
job_queue_id: 1
testing pid: 6242
to write: 4:6242,1
================
last entry: 4:6242,1;canceled
================
############
pid str: 5:6855,1
############
running_job_id: 5
job_queue_id: 1
testing pid: 6855
to write: 5:6855,1
================
last entry: 5:6855,1;canceled
================
############
pid str: 8:7218,1
############
running_job_id: 8
job_queue_id: 1
testing pid: 7218
to write: 8:7218,1
================
last entry: 8:7218,1;canceled
================
############
pid str: 9:4952,2
############
running_job_id: 9
job_queue_id: 2
testing pid: 4952
to write: 9:4952,2
================
last entry: 9:4952,2;canceled
================
############
pid str: 10:5098,3
############
running_job_id: 10
job_queue_id: 3
testing pid: 5098
to write: 10:5098,3
================
last entry: 10:5098,3;canceled
================
size of vector: 7
vector at 0: ▒▒▒U;canceled
vector at 1: 3:6157,1;canceled
vector at 2: 4:6242,1;canceled
vector at 3: 5:6855,1;canceled
vector at 4: 8:7218,1;canceled
vector at 5: 9:4952,2;canceled
vector at 6: 10:5098,3;canceled
iterator: ▒▒▒U;canceled
iterator: 3:6157,1;canceled
iterator: 4:6242,1;canceled
iterator: 5:6855,1;canceled
iterator: 8:7218,1;canceled
iterator: 9:4952,2;canceled
iterator: 10:5098,3;canceled
range: ▒▒▒U;canceled
range: 3:6157,1;canceled
range: 4:6242,1;canceled
range: 5:6855,1;canceled
range: 8:7218,1;canceled
range: 9:4952,2;canceled
range: 10:5098,3;canceled

I have tired Deque, Deque did not solve the problem.

I believe this may be an Iterator validity issue, not sure how to fix to it while using a vector or deque, which I receive the same error with deque

Aucun commentaire:

Enregistrer un commentaire