[erlang-questions] Tail recursion and memory leak

ok <>
Sun Mar 18 23:47:59 CET 2007


A word of advice.

On 16 Mar 2007, at 11:55 pm, Peter Lemenkov wrote:

> -module(test).
> -export([go/0]).
>
> go () ->
>         case gen_udp:open(3456) of
>                 {ok, Fd} ->
>                         select_calls(Fd,0),
>                         gen_udp:close(Fd);
>                 Error ->
>                         io:format("Error creating socket\n", []),
>                         exit(Error)
>         end.
>
> select_calls(Fd,NumberOfCalls) ->
>         io:format("Call number ~w~n~n",[NumberOfCalls]),
>         send_msg
> (Fd,"Caller","Called","UserName","SessionID","TagTo","TagFrom","Routet 
> o"),
>         send_msg
> (Fd,"Called","Caller","UserName","SessionID","TagFrom","TagTo","RouteF 
> rom"),
>         select_calls(Fd,NumberOfCalls+1).
>
> send_msg (Fd,Caller,Called,UserName,SessionID,TagTo,TagFrom,Route) ->
>        ok = gen_udp:send(Fd,"127.0.0.1",5060, "Hello All\n\r").
>

You would get much better line breaks if you put spaces after  
function argument
commas.  Let's see it that way.

select_calls(Fd, Number_Of_Calls) ->
	io:format("Call number ~w~n~n", [Number_Of_Calls]),
	send_msg(Fd, "Caller", "Called", "UserName", "SessionID",
		 "TagTo", "TagFrom", "Routeto"), % should that be "RouteTo"?
	send_msg(Fd, "Called", "Caller", "UserName", "SessionID",
		 "TagFrom", "TagTo", "RouteFrom"),
	select_calls(Fd, Number_Of_Calls + 1).

Tail recursion is fine, however I note that select_calls/2 can NEVER  
STOP, but you
have a call to gen_udp:close(Fd) following the initial call to  
select_calls(Fd, 0),
which only makes sense if it DOES eventually stop.  Which is it?   
When if ever is
Fd supposed to be closed?




More information about the erlang-questions mailing list