From: matz@... (Yukihiro Matsumoto) Date: 2002-07-05T00:28:07+09:00 Subject: Re: Mem leaks in rb_str_become? Hi, In message "Mem leaks in rb_str_become?" on 02/07/04, Michal Rokos writes: | I'm not sure is following code doesn't mem leaks... |- VALUE str = rb_obj_alloc(klass); |- |+ VALUE str; |+ | if (len < 0) { | rb_raise(rb_eArgError, "negative string size (or size too big)"); | } |- |+ |+ str = rb_obj_alloc(klass); OK. | if (ptr) { | memcpy(RSTRING(str)->ptr, ptr, len); |+ } else { |+ memset(RSTRING(str)->ptr, '\0', len); | } OK, but using MEMZERO() macro. | str_new3(klass, str) | VALUE klass, str; | { |- VALUE str2 = rb_obj_alloc(klass); |- |+ VALUE str2; |+ |+ Check_Type(str, T_STRING); str should alway be T_STRING, so no check is needed here. |+ Check_Type(orig, T_STRING); ditto. |- str = rb_str_buf_new(len + STR_BUF_MIN_SIZE); |+ str = rb_str_buf_new(len); OK. | if (NIL_P(str2)) { |- RSTRING(str)->ptr = 0; |+ RSTRING(str)->ptr = 0; /* FIXME: MEM LEAKs here ???*/ Yes. It was leaking. I should have moved if (!FL_TEST(str, ELTS_SHARED)) free(RSTRING(str)->ptr); before updating RSTRING(str)->ptr. Thank you for the changes. I will commit them. matz.