开发者

Remote Procedure Call cleanup

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(c开发者_开发知识库har* 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜