在Erlang中如何解释undef错误?

8wtpewkr  于 2022-12-08  发布在  Erlang
关注(0)|答案(1)|浏览(301)

I am trying to make sense of the following error:

4> c(sv).
{ok,sv}
5> B=sv:start().
<0.93.0>
6> =ERROR REPORT==== 14-Jul-2020::21:44:13.256000 ===
Error in process <0.93.0> with exit value:
{undef,[{common,createProcess,
                [{monitor,monitor,
                          {monstate,undefined,undefined,undefined,true,
                                    undefined,false}}],
                []},
        {sv,server,1,[{file,"sv.erl"},{line,14}]}]}

I have a sv module that instantiates a process from monitor module which in turn instantiates a process from worker module. Instantiantions take place via a call to the common module. Both sv and monitor use records defined in a .hrl module.

sv.erl -> monitor.erl -> worker.erl
 |            |
   common.erl
   records.hrl

common.erl

%%% common functionality module
-module(common).
-export([createProcess/1]).
-import(worker,[worker/1]).
-import(monitor,[monitor/1]).
-include("records.hrl").

createProcess({M,F,A})->
    Pid=spawn(M,F,[A]),
    Ref=erlang:monitor(process,Pid),
    {Pid,Ref}.

records.hrl

-record(monstate,{
    queue,
    qc,
    wpid,
    free=true,
    wref,
    init=false
}).

-record(sstate,{
    init=false,
    mpid=null,
    mref=null
}).

sv.erl

-module(sv).
-import(common,[createProcess/1]).
-include("records.hrl").
-export([start/0,server/1]).


start()->
    spawn(?MODULE,server,[#sstate{init=false}]).

server(State=#sstate{init=I})when I=:=false ->
    {MPid,MRef}=createProcess({monitor,monitor,#monstate{init=false}}),
    server(State#sstate{init=true,mpid=MPid,mref=MRef});

server(State=#sstate{mpid=MPid,mref=MRef})->
    receive
           {From,state}->From ! State,
                            server(State);
           {From,Message}-> MPid ! {request,{From,Message}},
                            server(State);
                
            {'DOWN',MRef,process,MPid,_}-> {NewMPid,NewMRef}=createProcess({?MODULE,monitor,#monstate{init=false}}),
                                            server(State#sstate{mpid=NewMPid,mref=NewMRef});
            _ ->exit(invalid_message)
                                    
    end.

monitor.erl

%%% servers as both a gen_server that can be queried  and a supervisor for its worker process
-module(monitor).
-export([monitor/1]).
-import(common,[createProcess/1]).

-include("records.hrl").

-define(QUEUE_SIZE,5).

tryEnqueue(Message,MState=#monstate{queue=Q,qc=C}) when C<?QUEUE_SIZE->
    NewQueue=queue:in(Message,Q),
    {queued,MState#monstate{qc=C+1,queue=NewQueue}};
tryEnqueue(_,MState)->{queue_full,MState}.

monitor(MState=#monstate{wpid=_,wref=_,init=I}) when I=:= false ->
    {WorkerPid,WorkerRef}=createProcess({worker,worker,self()}),
    monitor(MState#monstate{wpid=WorkerPid,wref=WorkerRef,init=true,qc=0,queue=queue:new()});

monitor(MState=#monstate{wpid=W,free=Free,wref=Ref,queue=Q,qc=C})->
    receive
        
        {request,{From ,Message}} -> case Free of 
                                            true -> W ! {From,Message},
                                                    monitor(MState#monstate{free=false});
                                                                             
                                            false -> 
                                                     St=case tryEnqueue({From,Message},MState) of 
                                                            {queue_full,S} -> From ! {queue_full,Message},S;
                                                            {queued,S} -> S
                                                        end,
                                                      monitor(St)
                                      end;
                                         
                                        
                                      
                                  
        {worker,{finished,_}}-> case queue:out(Q) of
                                    {{_,Element},Rest} -> W ! Element,
                                                          monitor(MState#monstate{free=false,queue=Rest,qc=C-1});
                                    {empty,Rest}       -> monitor(MState#monstate{free=true,queue=Rest})
                                end;

        {'DOWN',Ref,process,_,_}->
             {NewWorkerPid,NewWorkerRef}=createProcess({?MODULE,worker,self()}),
             monitor(MState#monstate{wpid=NewWorkerPid,wref=NewWorkerRef,free=true});

        _->exit(invalid_message)

    end.

So i do not know how to interpret the error:

  • there was a call to the common module , that had a MFA of : [{monitor,monitor,{monstate,undefined,undefined,.....}] . The content inside the { } looks okay but i do not get the encompassing [ ] .
    P.S My common module that spawns processes from both the sv and the monitor module has those modules imported and their associated records included too.I do not understand the problem.
vfwfrxfs

vfwfrxfs1#

该错误指出common:createProcess/1未定义。发生这种情况的原因很可能是模块common之前没有加载到代码服务器中。
也运行一个c(common).
请记住,-import编译指令仅表示Calls in this module to the funcion X/Y are to be performed by the module Module as Module:X/Y,而不创建模块依赖项

相关问题