<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">Hi,<br>
<br>There’s some strange behaviour with local funs and code loading in the VM that I don’t understand.<br><br><div>If I create a local fun in a process (e.g. something like F = <font face="Menlo">fun () -> … end</font> and not like F = <font face="Menlo">fun foo:bar/1</font>) the fun will be bound to my process (<font face="Menlo">erlang:fun_info(F, pid)</font> will return the creating process), and also to the current code version of the module where the fun was defined. If I load a new version of the module, the fun will point to old code. If I load an other new version of the module, the fun will no longer has its code in memory.</div><div><br></div><div>At this point:</div><div><ul class="MailOutline"><li>The process that created the fun is killed (I believe even if it cannot access the fun anymore, but it’s not yet garbage collected).</li><li>If I sent the fun to an other process, it is killed as well.</li><li>However, if I stored the fun in an ets table, the table survives the code loading. I can even retrieve the fun in a third process, call <span style="font-family: Menlo;">erlang:fun_info/2</span> on the fun there etc. If I try to invoke the fun however I get a <font face="Menlo">badfun</font> error.</li></ul><div><br></div></div><div>My question is why’s the difference? This is very confusing: some processes having the fun are killed, some others are not. On the surface the two funs look equal, but are they implemented in a different way somehow? I could imagine that the “killing fun” stores an actual pointer to the code (on the emulator level) for efficiency reasons, while the ets version stores only the hash of the module or something similar. But how does the fun in the process that retrieved the value from the ets table look like than?</div><div><br></div><div>And if a process can have a fun that has no corresponding code why are processes killed at all (assuming they are not executing the old code at the moment)? Why aren’t their invokable funs simply replaced with this non-invokable fun? (If I’m not mistaken funs are stored in an off-heap memory area, so you wouldn’t even have to search the process’ entire heap to find them.)</div><div><br></div><div>Could someone explain to me what is the VM doing here and why?</div><div><br></div><div>Thanks,</div><div>Daniel</div></body></html>