memory leakage problem in erl_format

Serge Aleynikov serge@REDACTED
Fri May 20 15:20:23 CEST 2005

I am not quite clear on your use of CreateMutex() and CloseHandle().  If 
the intent is to protect the call to erl_* functions to make 
send_5to_enode() thread-safe, I would suggest you to create/destroy the 
mutex outside of this function, and pass it as a parameter (or use a 
global variable), such as:


int initialize() {
   return (mutex != NULL);

void finalize() {
   if (mutex != NULL) {
     mutex = NULL;

Here's the snippet from MSDN about the name collisions in the mutex 
names (lpName):

"If lpName matches the name of an existing event, semaphore, waitable 
timer, job, or file-mapping object, the function fails and the 
GetLastError function returns ERROR_INVALID_HANDLE. This occurs because 
these objects share the same name space."

This might be contributing to the problem you are seeing.


maruthavanan s wrote:
> hi!
> I am using erl_interface in windows enviroment with otp R10B-2. i am 
> using the following function to send messages from c exe to erlang node.
> void send_5to_enode(int e1,int e2,int e3,int e4, int e5)
> {
> ETERM *t1;
> int Result;
> HANDLE mutex=NULL;
> DWORD result=0;
> mutex=CreateMutex(NULL,FALSE,"erlang3");
> result=WaitForSingleObject(mutex,INFINITE);
> t1=erl_format("{~i,~i,~i,~i,~i}",e1,e2,e3,e4,e5);
> Result=erl_reg_send(sockfd, "enodereceiver", t1);
> erl_free_compound(t1);
> erl_eterm_release();
> ReleaseMutex(mutex);
> CloseHandle(mutex);
> }
> If the load is heavy then this function is rapidly called and at certain 
> times we get a error saying unhandled exception. Later we debug and 
> found that when erl_format is called that internally calls some memory 
> alloc function and we get error in this point, also some times we get 
> error exception in erl_reg_send(). we used function 
> erl_eterm_statistics() to find the allocated and freed memory blocks and 
> some times it shows that allocated block as -1.
> Anything to be changed on the above code?
> Please help.
> regards,
> Maruthavanan.S

More information about the erlang-questions mailing list