diff --git a/ChangeLog b/ChangeLog index d5556b5cbb..d3261cd6fa 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Sat Nov 14 21:54:46 2009 Yusuke Endoh + + * class.c (rb_mod_init_copy): fix memory leak of Class#dup. + [ruby-dev:39687] + Sat Nov 14 17:09:39 2009 Nobuyoshi Nakada * configure.in (--with-opt-dir): ignore and suppress a warning. diff --git a/class.c b/class.c index a97494b8d7..056e1715be 100644 --- a/class.c +++ b/class.c @@ -151,6 +151,9 @@ rb_mod_init_copy(VALUE clone, VALUE orig) if (RCLASS_IV_TBL(orig)) { ID id; + if (RCLASS_IV_TBL(clone)) { + st_free_table(RCLASS_IV_TBL(clone)); + } RCLASS_IV_TBL(clone) = st_copy(RCLASS_IV_TBL(orig)); CONST_ID(id, "__classpath__"); st_delete(RCLASS_IV_TBL(clone), (st_data_t*)&id, 0); @@ -159,6 +162,11 @@ rb_mod_init_copy(VALUE clone, VALUE orig) } if (RCLASS_M_TBL(orig)) { struct clone_method_data data; + + if (RCLASS_M_TBL(clone)) { + extern void rb_free_m_table(st_table *tbl); + rb_free_m_table(RCLASS_M_TBL(clone)); + } data.tbl = RCLASS_M_TBL(clone) = st_init_numtable(); data.klass = clone; st_foreach(RCLASS_M_TBL(orig), clone_method, diff --git a/gc.c b/gc.c index f472bfc734..9de079be14 100644 --- a/gc.c +++ b/gc.c @@ -1459,8 +1459,8 @@ free_method_entry_i(ID key, rb_method_entry_t *me, st_data_t data) return ST_CONTINUE; } -static void -free_m_table(st_table *tbl) +void +rb_free_m_table(st_table *tbl) { st_foreach(tbl, free_method_entry_i, 0); st_free_table(tbl); @@ -1988,7 +1988,7 @@ obj_free(rb_objspace_t *objspace, VALUE obj) case T_MODULE: case T_CLASS: rb_clear_cache_by_class((VALUE)obj); - free_m_table(RCLASS_M_TBL(obj)); + rb_free_m_table(RCLASS_M_TBL(obj)); if (RCLASS_IV_TBL(obj)) { st_free_table(RCLASS_IV_TBL(obj)); }