C++ - gsoap:参数传递内存管理问题
我正在为其编写一个网络服务器和客户端测试存根。 我对参数的内存管理有疑问。
我从我的客户端调用一个肥皂函数 ns1_func1(input * pInput, output* pOutput) 现在输入和输出类都包含指向其他结构的指针。
对于例如
类输出 { 类 abc * p1; 类 def * p2; };
我的问题是 - 谁负责内存分配? 客户端负责输入内存分配,服务器负责输出内存管理吗?
现在我的客户端代码看起来像这样,
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
}
...
}
尽管使用soap_new_Output(soap, -1)从服务器分配pOutput,但在调用ns1_func1后我总是将pOutput设置为NULL。
另外,我的理解是,我们应该使用soap_new_X来分配内存,当我们调用soap_destroy时,内存会自动释放。 如果我错了,请纠正我。
基本上,我不知道在这种情况下谁应该负责内存分配/释放。
任何帮助都会很棒。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因为客户端和服务器一般是不同的进程,或者不同的机器,所以他们各自负责自己的内存管理。 客户端必须为其输入参数分配内存,然后 gsoap 将其序列化以发送到服务器。
服务器反序列化输入参数,分配所需的任何内存。 它为其输出分配内存,gsoap 将其序列化以发送回客户端。 客户端反序列化服务器的响应,分配所需的任何内存。
您肯定需要使用soap_malloc(等)进行内存分配,这是gsoap 库可以跟踪SOAP 调用清理时需要释放的内容的唯一方法。
在您提供的特定 ns1_func1 示例中,服务器分配响应,生成的客户端代码应该分配它所需的任何内存。 该调用的 WSDL 中可能存在问题,生成的客户端代码不是您所期望的。
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.
谢谢丹顿,
我面临的问题是在服务器中,我使用soap_new_abc和soap_new_def为类abc和def分配内存。 这些在清理过程中不会被释放。 在清理部分,我调用soap_destroy、soap_end 和soap_free。 根据 gsoap 文档,soap_free 应该调用通过soap_new_X 分配的类的~tors,但这并没有发生。 实际上,我将 DEBUG 语句放在soapC.cpp 的ctors 和~tors 中。 ctor 中的 DEBUG 语句出现,但 ~tors 中的 DEBUG 语句不出现。 所以我担心可能会出现内存泄漏。
在从soap_free清理期间,soap_delete_X不应该被自动调用吗?
Thanks Denton,
The problem I am facing is that in the server, I allocate memory for classes abc and def using soap_new_abc and soap_new_def. These are not freed during clean up. In clean up section, I am calling soap_destroy, soap_end and soap_free. As per gsoap documentation, soap_free should call the ~tors of classes allocated through soap_new_X but thats not happening. I actually put DEBUG statements in the ctors and ~tors in soapC.cpp. The DEBUG statements in ctor are appearing but not those in ~tors. So I fear there may be memory leaks.
Isnt soap_delete_X supposed to be called automatically during clean up from soap_free?
ns1_func1 的签名是什么? 是 ns1_func1( 输入*, 输出*) 还是 ns1_func1( 输入*, 输出*& ) ? 对于第一种情况,您将永远无法获得非空值。
What is the signature of ns1_func1? Is it ns1_func1( Input*, Output*) or ns1_func1( Input*, Output*& ) ? For the first case you would never be able to get a non-null value.
执行此代码后:
无论 ns1_func 做什么,pOutput 将始终为 NULL。 您将 pOutput 的值传递给函数,在本例中为 NULL。 如果不知道 pOutput(写为 &pOutput)的地址,该函数无法更改该值。
ns1_func1 要求一个指向“输出”结构的指针,因为这是它想要写入输出数据的地方。 因此,这意味着您需要在堆栈上
或堆上分配该空间:
如果 ns1_func1 要为您分配内存,则它必须返回指向输出结构的指针。 为此,它需要询问该指针的地址,或指向指针的指针。 换句话说,这样的声明:
抱歉,如果这有点令人困惑,所有的讨论都涉及指针到指针,但是您问题的基本答案是您必须为函数分配内存写入是因为该函数要求数据的地址,而不是指向数据的指针的地址。
After executing this code:
pOutput will always be NULL, no matter what ns1_func does. You are passing the value of pOutput to the function, which in this case is NULL. The function has no way to change that value without knowing the address of pOutput (written as &pOutput).
ns1_func1 asks for a pointer to an "output" struct because that's where it wants to write the output data. So that means you need to allocate that space, either on the stack:
or on the heap:
If ns1_func1 was going to allocate the memory for you, it would have to return a pointer to the output struct. To do that, it would need to ask for the address of that pointer, or a pointer to a pointer. In other words, a declaration like:
Sorry if this is a little confusing, with all the talk of pointers to pointers, but the basic answer to your question is that you must allocate the memory for the function to write to because the function is asking for the address of the data and NOT the address of a pointer to the data.