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

From: nobu@...
Date: 2015-10-29 15:01:11 UTC
List: ruby-core #71269
Issue #11626 has been updated by Nobuyoshi Nakada.


GC Yang wrote:
> Got it, I should have set the pointer to NULL.

No, you should not depend on the content of `malloc`ed buffer.

----------------------------------------
Bug #11626: fprintf in c extension keep adding memory usage
https://bugs.ruby-lang.org/issues/11626#change-54642

* Author: GC Yang
* Status: Rejected
* Priority: Normal
* Assignee: 
* ruby -v: 1.9.3p550
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
`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:

~~~c
VALUE leak (VALUE self, VALUE in) {
  char *str = StringValuePtr(in);
  char *pstr;
  pstr = malloc (10240);
  strcat(pstr,str);
  fprintf (stdout, pstr);
  self = rb_str_new_cstr(pstr);
  free (pstr);
  return self;
}

void Init_extension(){
  rb_define_global_function("leak", leak, 1);
}
~~~
~~~ruby
100000000.times{leak("lots of garbage")}
~~~

without the `fprintf` line the memory usage is normal, adding that line just keep adding memory, kill the console process will release the memory. 

If I do `100000.times{Process.wait(fork{ stuff})}`, memory also looks normal. Leaking somewhere???? 



-- 
https://bugs.ruby-lang.org/

In This Thread

Prev Next