Skip to content

Commit

Permalink
Call obj_free for T_DATA, T_FILE objects on exit
Browse files Browse the repository at this point in the history
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 <XrXr@users.noreply.github.com>
  • Loading branch information
peterzhu2118 and XrXr committed Dec 14, 2023
1 parent e363127 commit 912016f
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions gc.c
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();

Expand All @@ -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:
Expand Down

0 comments on commit 912016f

Please sign in to comment.