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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With