Fix memory leak in rb_const_remove when using namespace

We need to free the rb_const_entry_t we remove from the RCLASS_WRITABLE_CONST_TBL
otherwise it will leak memory.
This commit is contained in:
Peter Zhu 2025-10-18 14:32:25 -04:00
parent 2f20dc5dc5
commit 957c832db1
Notes: git 2025-10-19 14:22:55 +00:00

View File

@ -3533,7 +3533,8 @@ rb_const_remove(VALUE mod, ID id)
rb_check_frozen(mod);
ce = rb_const_lookup(mod, id);
if (!ce || !rb_id_table_delete(RCLASS_WRITABLE_CONST_TBL(mod), id)) {
if (!ce) {
if (rb_const_defined_at(mod, id)) {
rb_name_err_raise("cannot remove %2$s::%1$s", mod, ID2SYM(id));
}
@ -3541,6 +3542,14 @@ rb_const_remove(VALUE mod, ID id)
undefined_constant(mod, ID2SYM(id));
}
VALUE writable_ce = 0;
if (rb_id_table_lookup(RCLASS_WRITABLE_CONST_TBL(mod), id, &writable_ce)) {
rb_id_table_delete(RCLASS_WRITABLE_CONST_TBL(mod), id);
if ((rb_const_entry_t *)writable_ce != ce) {
xfree((rb_const_entry_t *)writable_ce);
}
}
rb_const_warn_if_deprecated(ce, mod, id);
rb_clear_constant_cache_for_id(id);