[ruby-core:71232] Re: [Ruby trunk - Bug #11626] [Open] fprintf in c extension keep adding memory usage

From: Eric Wong <normalperson@...>
Date: 2015-10-27 22:12:58 UTC
List: ruby-core #71232
guchen.yang@fugawi.com wrote:
> fprintf will keep adding memory usage without releasing it. 
> 
> I was doing it in rails console, not sure if it makes any different. The code like this:
> 
> VALUE leak (VALUE self, VALUE in) {
>   char *str = StringValuePtr(in);
>   char *pstr;
>   pstr = malloc (10240);
>   strcat(pstr,str);

pstr is uninitialized memory when you call strcat on it,
likely screwing with how fprintf uses it.
Does replacing the malloc + strcat with:

	pstr = strdup(str);

help at all?

>   fprintf (stdout, pstr);
>   self = rb_str_new_cstr(pstr);
>   free (pstr);
>   return self;
> }

In This Thread

Prev Next