memory leakage problem in erl_interface

maruthavanan s <>
Sun Mar 6 14:04:18 CET 2005


Hi,

I have built a c exe with erl interface. When I send more messages from 
erlang to c or c to erlang my  exe keeps on consuming memory drastically, 
which leads to memory leakage problem.

Here are my codes.

How to reduce my memory usage.

Please help! I am in hurry





int main(int argc, char **argv)

{

  int id_number = 1;

  int creation=1,msg_from_enode(ETERM *);

  char *cookie="secretcookie";

  char *nodename="";

  int loop = 1;                            /* Loop flag */

  int got;                                 /* Result of receive */

  unsigned char buf[BUFSIZE];              /* Buffer for incoming message */

  ErlMessage emsg;                         /* Incoming message */

  HANDLE mutex=NULL;

  ETERM *fromp, *tuplep, *fnp, *argtuple;



erl_init(NULL, 0);



if( erl_connect_init(id_number, cookie, (short)creation) == -1)

            erl_err_quit("erl_connect_init");

if ((sockfd = erl_connect(nodename)) < 0)

erl_err_quit("ERROR: erl_connect failed");

printf("Connection established...\n");

erl_eterm_release();

while (loop)

  {

            memset(&emsg,0,sizeof(emsg));

            got = erl_receive_msg(sockfd, buf, BUFSIZE, &emsg);

if (got == ERL_TICK)

{

}

else if (got == ERL_ERROR)

            {

                        loop = 0;

}

            else

            {

  switch (emsg.type)

              {

              case ERL_REG_SEND:

                                    mutex=CreateMutex(NULL,FALSE,"erlang3");

                                    FileIP=fopen("input.txt","a");

                                    WaitForSingleObject(mutex,INFINITE);

                                    erl_print_term(FileIP,emsg.msg);

                                    fclose(FileIP);

                                    fromp    = erl_element(2, emsg.msg);

                                    tuplep   = erl_element(3, emsg.msg);

                                    fnp                    = erl_element(1, 
tuplep);

                                    argtuple = erl_element(2, tuplep);

                                    if (strncmp(ERL_ATOM_PTR(fnp), "enode", 
5) == 0)

                                    {

                                                int i=0,tl;

                                                tl = 
ERL_TUPLE_SIZE(argtuple);



                                                msg_from_enode(argtuple);

                                    }

                                    erl_free_compound(emsg.from);

                                    erl_free_compound(emsg.msg);

                                    erl_free_compound(fromp);

                                    erl_free_compound(tuplep);

                                    erl_free_compound(fnp);

                                    erl_free_compound(argtuple);

                                    erl_eterm_release();

                                    ReleaseMutex(mutex);

                                    CloseHandle(mutex);

                                    break;

              case ERL_EXIT:

                                    break;

              }

            }

  }

                        printf("ERL Got error");

                        getch();



}



after receiving the message from C we are calling some functions in C



void msg_from_enode(ETERM *tuplep)

{

function_number=ERL_INT_VALUE(erl_element(1,tuplep));



/*based up on the function number we are parsing the parameters

from the tuple and we are calling the functions in C*/



erl_free_compound(tuplep);

            //printf("Compound Released\n");

}





For sending message from C to erlang we use the following code:



void send_3to_enode(int e1,int e2,int e3)

{

ETERM *t1;

int Result;

DWORD result=0;

            HANDLE mutex=NULL;

            mutex=CreateMutex(NULL,FALSE,"erlang3");

            result=WaitForSingleObject(mutex,INFINITE);

            t1=erl_format("{~i,~i,~i}",e1,e2,e3);

            File3P=fopen("file3.txt","a");

            erl_print_term(File3P,t1);

            Result=erl_reg_send(sockfd, "enodereceiver", t1);

            erl_free_compound(t1);

            erl_eterm_release();

            ReleaseMutex(mutex);

            CloseHandle(mutex);

            fclose(File3P);

}

thanks,
Maruthavanan

_________________________________________________________________
On the road to retirement? Check out MSN Life Events for advice on how to 
get there! http://lifeevents.msn.com/category.aspx?cid=Retirement




More information about the erlang-questions mailing list