[erlang-questions] Weird behaviuor of ei_x_free

Sverker Eriksson sverker.eriksson@REDACTED
Wed Mar 25 16:19:56 CET 2015

On 03/24/2015 10:09 AM, Frans Schneider wrote:
> Hi list,
> While writing a simple port program in C, I ran in a problem with 
> deallocating ei_x_buff results. After a few calls to the port, 
> ei_x_new_with_version would crash with a malloc error.
> It turned out that when I replace the call
> ei_x_free(&result);
> with
> r = ei_x_free(&result);
> the problem is solved.
> This is what the code looks like:
> static void process_helo(const byte *_buf, int _index_start) {
>   ei_x_buff result;
>   ei_x_new_with_version(&result);
>   ei_x_encode_atom(&result, "ok");
>   write_port_msg(result);
>   ei_x_free(&result);
> }
> Could somebody explain what is going on here?
I can try.

Change your 'write_port_msg' function to take a *pointer" to ei_x_buff
and change the call to pass a pointer to 'result': write_port_msg(&result).

Your code passes 'result' by value and thereby gives write_port_msg a 
shallow copy
of 'result'.

/Sverker, Erlang/OTP

More information about the erlang-questions mailing list