Home > Blockchain >  Why is the value not transferred?
Why is the value not transferred?

Time:11-12

First time using GRPC. I am calling my Stat function, which should return info on a File I am interested in. When I call the Stat function from within the parent Fetch function, I get a response value of 0. Here is what my code looks like:

StatusCode DFSClientNodeP1::Stat(const std::string &filename, void *file_status)
{
    StatusCode return_status;

    grpc::ClientContext context;
    dfs_service::File request;
    dfs_service::File_Metadata response;

    request.set_filename(this->WrapPath(filename));
    this->service_stub->get_status(&context, request, &response);

    std::cout << "SHOULD BE " << response.filesize() << std::endl;

    file_status = &response;

    return return_status;
}


StatusCode DFSClientNodeP1::Fetch(const std::string &filename)
{
    StatusCode return_status;
    dfs_service::File_Metadata response;

    return_status = Stat(filename, &response);

    std::cout << "WHAT IT IS " << response.filesize() << std::endl;

    return StatusCode::OK;
}

Here is the output:

SHOULD BE 232762 
WHAT IT IS 0

I am just curious where I am going wrong here. I tried to be very particular and static_cast the void* handling, but when I did that I got very large numbers, so it seemed the memory did not exist. For that reason I believe it is a memory persistence issue. I also read GRPC documentation, seems that Messages do not persist when the message transfer completes. Is that what is happening here? Should I try to malloc the Message type, seems not right to me.

CodePudding user response:

file_status = &response; will result in the outer response to be a dangling pointer when called in Fetch since you are taking the address of a function-local variable that does no exist outside the scope of the Stat function

Instead you should modify the pointer that was passed into Stat

StatusCode DFSClientNodeP1::Stat(const std::string &filename, void *file_status)
{
    StatusCode return_status;

    grpc::ClientContext context;
    dfs_service::File request;
    auto response = static_cast<dfs_service::File_Metadata*>(file_status);

    request.set_filename(this->WrapPath(filename));
    this->service_stub->get_status(&context, request, response);

    std::cout << "SHOULD BE " << response->filesize() << std::endl;

    return return_status;
}
  • Related