'function not exported' in erlang

This is my code when I run: gen_server_test: alloc, it gives me an error, please help me

-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").

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
+3
source share
2 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 handle_call to:

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

will "solve" the problem now. But I recommend looking at the gen_server documentation in general, because you have other weird things. For example, your condition is the result ìo:format("channels"). That doesn't make any sense.

, gen_servers, , , .

+10

handle_call handle_cast {reply, Response, NewState} {noreply, NewState}. gen_server .

io:format(..) ok.., , .

+1

All Articles