Segmentation Fault in gSoap,The inferior stopped because it received a signal from the Operating System

StackOverflow https://stackoverflow.com/questions/17325617

  •  01-06-2022
  •  | 
  •  

Question

I have a pointer and when i dereference it, it gives me error. The problem is that the parameter *ns__personRequestResponse

int __ns3__PersonRequest(soap *, _ns1__PersonRequest *ns1__PersonRequest, _ns1__PersonRequestResponse *ns1__PersonRequestResponse)
{
    ns1__PersonRequestResponse->result = 0;
    //ns1__PersonRequestResponse = new _ns1__PersonRequestResponse();
    *ns1__PersonRequestResponse->result = 39; // Error here
    return SOAP_OK;
}

Below is the part of header file created from wsdl that has response parameter.

class _ns1__PersonRequestResponse
{ 
  public:
     /// Element result of type xs:int.
     int*                                 result                         0; ///< Nullable pointer.
     /// A handle to the soap struct that manages this instance (automatically set)
     struct soap                         *soap                          ;
};

I get segmentation fault when i assign a value to an integer variable result. How i can get it working?

Was it helpful?

Solution

The result field in your structure is a pointer to int. In your code, you first initialize it to 0, then attempt to assign a value through that pointer. But on most systems, that will fail, because the memory at address 0 hasn't been allocated to your program.

The fix is to ensure that result points to valid memory before attempting to assign through that pointer. Exactly how that should happen depends on how that structure is going to be used in your code. One way would be to declare an int variable outside of your function (probably at file scope), then take its address and assign it to result:

int my_result;  // This should be declared OUTSIDE of a function!

int __ns3__PersonRequest(soap *, _ns1__PersonRequest *ns1__PersonRequest, _ns1__PersonRequestResponse *ns1__PersonRequestResponse)
{
    ns1__PersonRequestResponse->result = &my_result;  // Make sure pointer points to valid memory
    *ns1__PersonRequestResponse->result = 39; // Updates the value of my_result
    return SOAP_OK;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top