远程过程调用清理
我的情况如下: 我用 C 实现了一个简单的 RPC 系统,客户端可以通过该系统调用向其传递命令的远程函数。该命令在远程计算机上执行,并将包含输出的字符串返回给客户端。因此,函数签名是:
char* execute(char* command)
在这个函数内,我对结果字符串进行 malloc 并返回它。 该函数已发布,客户端可以调用它。 我的问题是:每次远程过程调用后,我可以通过什么方式释放服务器端分配的 char* ?
编辑:要详细说明我的问题:这里的问题是在第一次 rpc 调用之后,服务器崩溃并出现“glibc 检测到 free() 无效指针”错误。在执行过程中,我有类似的内容:
char* result = (char*) malloc(STRING_SIZE * sizeof(char));
...
return result;
我假设它正在尝试释放返回的结果,但失败了。
My situation is the following:
I've implemented a simple RPC system in C through which a client can call a remote function to which it passes a command. The command is executed on the remote machine and the string containing the output is returned to the client. So, the function signature is:
char* execute(char* command)
Inside this function I do a malloc for the result string and return it.
The function is published and clients my invoke it.
My question is: in which way can I free the allocated char* on the server side after each remote procedure call?
Edit: To detail my problem a bit more: the issue here is that after the first rpc call, the server crashes with "glibc detected free() invalid pointer" error. Inside the execute procedure I have something like:
char* result = (char*) malloc(STRING_SIZE * sizeof(char));
...
return result;
I'm assuming it's trying and failing to free the returned result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于使用哪种RPC机制。如果函数被适当标记,有些函数可以自动调用返回指针的释放函数。
如果您的 RPC 库无法做到这一点,您必须将 malloc 更改为其他内容。在单线程服务器中,您可以简单地返回指向静态缓冲区的指针,在多线程中......这取决于情况。
It depends on which RPC mechanism is used. Some can automatically call freeing functions on returned pointers if the function is marked appropriately.
If your RPC library can't do that, you have to change malloc to something else. In single-threaded server, you can simple return pointer to a static buffer, in multithreaded...it depends on the situation.