View Single Post
  #7  
Old 11-02-2010, 06:54 PM
juppmaifeld juppmaifeld is offline
Member
 
Join Date: Aug 2009
Posts: 43
Default

Hi Kornalius, Hi Mike,

I have detected the same problem using the functions ARRAYTOSTR() and CHAR() in a thread. What I discovered is, that, if you use a function that returns a string the amount of memory the system needs increases with every call of the function. For example. I use Nicknacks tcp.ppl library. If I call the tcp_recv() function in a loop you can watch the memory growth in the windows task manager. If you eliminate the "return (char(buffer$));" like I have done it in Mytcp_recv just for test reasons the problem disappears. One can argue that the problem maybe comes from not freeing the buffer$ in "tcp_recv(s$, sz$)". But that's not the case. I checked that out by successivly commenting out parts of the code of tcp_recv() to figure out which part of the code causes the problem. The same problem occured using ARRAYTOSTR() in a thread. I wrote my own ARRAYTOSTR() procedure to avoid returning a string, and it worked fine. I am not sure, but from my experience with all that testing it appears to me that there is maybe a problem that causes a memory leak if a function returns a string.

Jupp


func tcp_recv(s$, sz$)

local(buffer$, readcnt$);
// PPL 1.53, 1.62 dosen't know var(buffer$, sz$);
SDIM(buffer$, TBYTE, sz$);

if (s$ == 0)
s$ = tcp_socket$;
end;

readcnt$ = recv(s$, &buffer$, sz$, 0);

if (readcnt$ < 0)
return ( - 1);
else
setbyte(&buffer$ + readcnt$, 0);
return (char(buffer$));
end;
end;


proc Mytcp_recv(s$, sz$, strData$, res$)

local(buffer$, readcnt$);

// PPL 1.53, 1.62 dosen't know var(buffer$, sz$);
SDIM(buffer$, TBYTE, sz$);

if (s$ == 0)
s$ = tcp_socket$;
end;

readcnt$ = recv(s$, &buffer$, sz$, 0);

if (readcnt$ < 0)
res$ = -1;
else
setbyte(&buffer$ + readcnt$, 0);
if (iswide(buffer$))
strData$ = char(buffer$);
else
strData$ = buffer$;
end;
res$ = 1;
end;

free(buffer$);
end;



proc MyArrayToStr(arr$, arrlength$, string$, mode$)

local(i$, s$);

s$ = "";
string$ = "";

if (mode$ == TCP_MODE)
if (arrlength$ == 1)
sprintf(string$, "%03u", arr$[0]);
else
for (i$, 0, arrlength$ - 1, 1)
if (i$ == arrlength$ - 1)
sprintf(s$, "%03u", arr$[i$]);
else
sprintf(s$, "%03u ", arr$[i$]);
end;
string$ = string$ % s$;
// showmessage("s$ = " % s$ % "\n" %
// "string$ = " % string$);
end;
end;

else

if (arrlength$ == 1)
sprintf(string$, "%u", arr$[0]);
else
for (i$, 0, arrlength$ - 1, 1)
sprintf(s$, "%u", arr$[i$]);
if (i$ == arrlength$ - 1)
string$ = string$ % s$;
else
string$ = string$ % s$ % " ";
end;
end;
end;
end;
end;
Reply With Quote