From 912016f626fab3e79e3ef8d9da53d1b148c8b5bf Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Wed, 13 Dec 2023 14:25:19 -0500 Subject: [PATCH] Call obj_free for T_DATA, T_FILE objects on exit Previously, T_DATA and T_FILE objects did not have their instance variables freed on exit which would be reported as a memory leak with RUBY_FREE_ON_EXIT. This commit changes it to use obj_free which also frees the generic instance variables. Co-authored-by: Alan Wu --- gc.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/gc.c b/gc.c index 14bbf64942a677..49e01bbd004969 100644 --- a/gc.c +++ b/gc.c @@ -4565,6 +4565,12 @@ gc_abort(rb_objspace_t *objspace) } } + for (int i = 0; i < SIZE_POOL_COUNT; i++) { + rb_size_pool_t *size_pool = &size_pools[i]; + rb_heap_t *heap = SIZE_POOL_EDEN_HEAP(size_pool); + rgengc_mark_and_rememberset_clear(objspace, heap); + } + gc_mode_set(objspace, gc_mode_none); } @@ -4602,7 +4608,7 @@ rb_objspace_free_objects(rb_objspace_t *objspace) switch (BUILTIN_TYPE(vp)) { case T_DATA: { if (rb_obj_is_mutex(vp) || rb_obj_is_thread(vp)) { - rb_data_free(objspace, vp); + obj_free(objspace, vp); } break; } @@ -4651,14 +4657,6 @@ rb_objspace_call_finalizer(rb_objspace_t *objspace) /* Abort incremental marking and lazy sweeping to speed up shutdown. */ gc_abort(objspace); - if (rb_free_on_exit) { - for (int i = 0; i < SIZE_POOL_COUNT; i++) { - rb_size_pool_t *size_pool = &size_pools[i]; - rb_heap_t *heap = SIZE_POOL_EDEN_HEAP(size_pool); - rgengc_mark_and_rememberset_clear(objspace, heap); - } - } - /* prohibit GC because force T_DATA finalizers can break an object graph consistency */ dont_gc_on(); @@ -4684,12 +4682,10 @@ rb_objspace_call_finalizer(rb_objspace_t *objspace) if (rb_obj_is_fiber(vp)) break; if (rb_obj_is_main_ractor(vp)) break; - rb_data_free(objspace, vp); + obj_free(objspace, vp); break; case T_FILE: - if (RANY(p)->as.file.fptr) { - make_io_zombie(objspace, vp); - } + obj_free(objspace, vp); break; case T_SYMBOL: case T_ARRAY: