Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'function not exported' in erlang

Tags:

erlang

This is my code, when i run: gen_server_test:alloc, it gives me the error, please help me out

-module(gen_server_test).
-behaviour(gen_server).
-export([start_link/0]).
-export([alloc/0, free/1]).
-export([init/1, handle_call/3, handle_cast/2]).
start_link() ->
    gen_server:start_link({local, gen_server_test}, gen_server_test, [], []).

alloc() ->
    io:format("allocing"),
        gen_server:call(?MODULE, {alloc}).

free(Ch) ->
    gen_server:cast(gen_server_test, {free, Ch}).

init(_Args) ->
    {ok, channels()}.

handle_call({alloc}, _From, LoopData) ->
    io:format("handle alloc").

handle_cast({free, Ch}, LoopData) ->
    io:format(Ch).

free() -> 
        io:format("free").

channels() ->
    io:format("channels").

the error:

23> gen_server_test:alloc().     
allocinghandle alloc
=ERROR REPORT==== 6-May-2011::18:05:48 ===
** Generic server gen_server_test terminating 
** Last message in was {alloc}
** When Server state == ok
** Reason for termination == 
** {'function not exported',
       [{gen_server_test,terminate,[{bad_return_value,ok},ok]},
        {gen_server,terminate,6},
        {proc_lib,init_p_do_apply,3}]}
** exception exit: undef
     in function  gen_server_test:terminate/2
        called as gen_server_test:terminate({bad_return_value,ok},ok)
     in call from gen_server:terminate/6
     in call from proc_lib:init_p_do_apply/3
like image 599
why Avatar asked Dec 22 '22 14:12

why


1 Answers

Here is the important part of the error report: {bad_return_value,ok} Valid return values for handle_call/3 are:

  {reply,Reply,NewState} 
| {reply,Reply,NewState,Timeout}
| {reply,Reply,NewState,hibernate}
| {noreply,NewState} 
| {noreply,NewState,Timeout}
| {noreply,NewState,hibernate}
| {stop,Reason,Reply,NewState} 
| {stop,Reason,NewState}

Rewriting your handle_call into:

handle_call({alloc}, _From, LoopData) ->
  Reply = io:format("handle alloc"),
  {reply, Reply, LoopData}

would "solve" the problem now. But I recommend going through the documentation of gen_server in general cause you have some other strange stuff going on. Your state for instance is the result of ìo:format("channels"). This doesn't make any sense at all.

I guess you're learning about gen_servers and that's great and I promise the documentation will help you solve a lot of minor problems along your path towards knowledge.

like image 185
D.Nibon Avatar answered Jan 03 '23 10:01

D.Nibon