zed.0xff.me
You MUST free memory you got from ALLOC_N & friends
It’s not documented anywhere, but you must call “xfree()” (note ‘x’ there) on memory blocks you got from ALLOC_N & friends.
Ruby will not free that memory automatically during it’s usual GC process.
Guys from ruby-talk and rubyforge also noticed that.
sample code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
VALUE method_parse(VALUE self, VALUE text) {
VALUE s;
char *p = RSTRING(text)->ptr;
in_buf_len = RSTRING(text)->len;
// [skipped some code here]
buf = ALLOC_N(char, bufsize);
// protect buf from GC (theoretically)
rb_iv_set(self,"@obj",Data_Wrap_Struct(rb_cData,NULL,NULL,buf));
// [skipped some hard work here]
// make ruby string from our char[] data
s = rb_str_new(buf,bufptr-buf);
// cleanup
rb_iv_set(self,"@obj",Qnil);
xfree(buf);
buf = NULL;
bufsize = 0;
return s;
}
|