- Subject: [slang-users] Memory leak found (w/solution) in: posix_read() in slposio.c module
- From: "Gonzalez, Jorge" <jgonzalez@xxxxxxxxxxxxxxxxx>
- Date: Mon, 28 Mar 2005 17:19:30 -0500
Hi,
I was doing some serial communication programming with slang 1.4.5 on
QNX4 and found and interesting behavior of the POSIX compatible read
function.
Every time I used that function to get the contents of the serial input
buffer, I noticed that my application leaked memory in 8K chunks. I had
a look at the implementation of the posix_read() function included in
the slposio.c module and found that a SLbstring_free() call was missing.
So when I put that in and recompiled the library the mem leak was gone.
I wonder if somebody else has noticed/fixed this before and I apologize
if that is the case, I might just be late with the news.
Otherwise, the fix goes as follows. Please look for the /*** MEM LEAK
FIX ***/ comments.
Thanks,
Jorge Gonzalez
P.S. I also checked the 1.4.9 version and it does the same thing. :-)
/* Usage: nn = read (f, &buf, n); */
static void posix_read (SLFile_FD_Type *f, SLang_Ref_Type *ref, unsigned
int *nbytes)
{
unsigned int len;
char *b;
SLang_BString_Type *bstr;
b = NULL;
len = *nbytes;
if ((-1 == check_fd (f->fd))
|| (NULL == (b = SLmalloc (len + 1))))
goto return_error;
if (-1 == f->read (f->fd, b, &len))
{
_SLerrno_errno = errno;
goto return_error;
}
if (len != *nbytes)
{
char *b1 = SLrealloc (b, len + 1);
if (b1 == NULL)
goto return_error;
b = b1;
}
bstr = SLbstring_create_malloced ((unsigned char *) b, len, 0);
if (bstr != NULL)
{
if ((-1 != SLang_assign_to_ref (ref, SLANG_BSTRING_TYPE,
(VOID_STAR)&bstr))
&& (-1 != SLang_push_uinteger (len)))
{ /*** MEM
LEAK FIX ***/
SLbstring_free (bstr); /*** MEM LEAK FIX
***/
return;
} /*** MEM
LEAK FIX ***/
SLbstring_free (bstr);
b = NULL;
/* drop */
}
return_error:
if (b != NULL) SLfree ((char *)b);
(void) SLang_assign_to_ref (ref, SLANG_NULL_TYPE, NULL);
(void) SLang_push_integer (-1);
}
_______________________________________________
To unsubscribe, visit http://jedsoft.org/slang/mailinglists.html
[2005 date index]
[2005 thread index]
[Thread Prev] [Thread Next]
[Date Prev] [Date Next]