[erlang-bugs] Memory leakage regarding enif_alloc_env() and iolists

Masatake Daimon <>
Fri Oct 28 07:03:56 CEST 2011


I found a memory leakage regarding enif_alloc_env() and iolists. The
function enif_inspect_iolist_as_binary() (and sometimes
enif_inspect_binary()) allocates a temporary object to be retained by
((ErlNifEnv*)env)->tmp_obj_list. This is OK for ordinary
process-dependent environments as such objects will later be
deallocated by either erts_post_nif() or post_nif_noproc(). But for
process-independent ones allocated by enif_alloc_env(), neither
enif_clear_env() nor enif_free_env() calls free_tmp_objs() so
temporary objects will never be freed, hence the memory leak.

So I attempted to fix the problem by applying the attached patch. The
patch seemingly cleared away the leakage but unfortunately it induced
the runtime system to randomly crash on the call of enif_free_env():

> Program terminated with signal 11, Segmentation fault.
> #0  0x000000000044275a in destroy_carrier (allctr=0x791440, blk=0x2b1776d98030)
>     at beam/erl_alloc_util.c:636
> 636     beam/erl_alloc_util.c: No such file or directory.
>         in beam/erl_alloc_util.c
> (gdb) bt
> #0  0x000000000044275a in destroy_carrier (allctr=0x791440, blk=0x2b1776d98030)
>     at beam/erl_alloc_util.c:636
> #1  0x0000000000443d67 in erts_alcu_free_thr_spec (type=<value optimized out>,
>     extra=<value optimized out>, p=0x2b1776d98038)
>     at beam/erl_alloc_util.c:2898
> #2  0x000000000051a6cd in enif_free_env (env=0x823c78) at beam/erl_nif.c:145
> << snip >>
> #7  0x000000000052b186 in process_main () at beam/beam_emu.c:3321
> #8  0x000000000049d5db in sched_thread_func (vesdp=<value optimized out>)
>     at beam/erl_process.c:3782
> #9  0x000000000059f589 in thr_wrapper (vtwd=<value optimized out>)
>     at pthread/ethread.c:106
> #10 0x00002b1777345143 in start_thread () from /lib64/libpthread.so.0
> #11 0x00002b177761e52d in clone () from /lib64/libc.so.6
> #12 0x0000000000000000 in ?? ()

The Erlang/OTP version I'm using is R14B04 while I highly suspect that
the leakage still remains in the Git repository's master branch. I'd
be glad if someone familiar with the internals of beam finds a proper
way to fix the problem.

Masatake Daimon
大門 正岳 <>

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: fix-nif-env-memleak.patch
URL: <http://erlang.org/pipermail/erlang-bugs/attachments/20111028/e76df846/attachment.ksh>

More information about the erlang-bugs mailing list