[erlang-questions] Dynamic code reloading

Jeffm <>
Tue Apr 20 08:46:45 CEST 2010

On 20/04/10 4:28 PM, Deryk Barker wrote:
> Jeffm wrote:
>> When you loaded the new module in the shell did you use c() or 
>> l(dbase)? If you compiled external to the shell, as you stated, 
>> you'll need to use l(module_name) to tell the shell to load the new 
>> version. 
> I tried c() which took down the server process. At your suggestion, I 
> just tried compiling externally and then used l().
> Which worked - the first time, i.e. when all I had done since starting 
> the erlang shell was spawn the server and register its name (which was 
> definitely in the list returned by registered()). When I recompiled 
> externally again and re-used l() it took down the server process...sigh.

At a guest... I saw a similar problem when I was developing a module. 
The problem is that the erlang vm only holds two versions of the code at 
once. This is perfectly alright until you have a mistake in your code 
like I did. The loop wasn't executing the new code and died when a third 
version was added. Try adding a message which will cause the module to 
print the current version of code. For example,


-define(V, 1).

   print_version ->
       io:format("Current version of ~p is ~p~n", [?MODULE, ?V]),

then after loading the new code sent it a message to see what the 
current version is.

A thought just occurred to me. Your probably stuck in the old code as 
the program hasn't progressed around the loop. Load the new version, 
trigger it to go round the loop, then load the third version and trigger 
it again. By trigger, I'm suggesting a message, but any event which 
causes it to go round the loop should work. It only starts the newly 
loaded code once it re-enters to loop.

> I don't understand what's going on here, my understanding of the 
> manual was that you can have two versions of a module loaded, the 
> current and the old, I should have expected, after the load, the 
> running server to be using the old. It successfully switched to the 
> newly loaded code, via the call dbase:loop(DataBase), the first time I 
> tried this, so - again, as I understand it - it was then using the 
> current.
> When I did another load of the module (again using l()), the old old 
> (as it were) is purged and the current becomes the old.
> So why, if my server process was now running the (about to be old) 
> current was it terminated? And why silently?
> Any insight gratefully received.
> deryk

More information about the erlang-questions mailing list