Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ - gsoap : Parameter passing memory management issues

I am writing a web server and client test stub for it. I have questions regarding memory management of the parameters.

From my client I am calling a soap function ns1_func1(input * pInput, output* pOutput) Now both input and output class contain pointers to other structs.

For e.g

class Output { class abc * p1; class def * p2; };

My question is - who is responsible for memory allocations? Is client responsible for input memory allocations and server responsible for output memory management?

Right now my client code looks somthing like this

client_fn()
{

 ...

 input inp1;
 output * pOutput = NULL;
 ns1_func1(&inp1, pOutput);
 if(pOutput == NULL)
 {
   cout<<"pOut is NULL\n";
   return ERR;
 }
 else
 {
   // retrive output values from pOutput
 }

 ...
}

I am always getting pOutput as NULL after call to ns1_func1 in spite of allocating pOutput from the server using soap_new_Output(soap, -1).

Also, my understanding is that we should use soap_new_X to allocate memory which gets automatically deallocated when we call soap_destroy. Pls correct me if I am wrong.

Basically I am struggling with no knowledge about who is supposed to take care of memory allocation/deallocation in such cases.

Any help would be great.


1 Answers

Because the client and server are generally different processes, or different machines, they are each responsible for their own memory management. The client must allocate the memory for its input parameters, which gsoap then serializes to send to the server.

The server deserializes the input parameters, allocating any memory it needs to do so. It allocates the memory for its output, which gsoap serializes to send back to the client. The client deserializes the server's response, allocate any memory it needs to do so.

You definitely need to use soap_malloc (et al) for memory allocations, thats the only way the gsoap libraries can track what needs to be freed when the SOAP call cleans up.

In the specific ns1_func1 example you gave, the server allocates the response and the generated client code is supposed to allocate any memory it requires. There may be something wrong in the WSDL for that call, that the client code being generated is not what you expect.

like image 180
DGentry Avatar answered Feb 14 '26 23:02

DGentry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!