Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mrb_gc_register() may cause GC and collect the object being protected #6317

Closed
catsidhe opened this issue Jul 31, 2024 · 2 comments
Closed

Comments

@catsidhe
Copy link

catsidhe commented Jul 31, 2024

mrb_gc_register() calls mrb_ary_new() or mrb_ary_push() - both of which may trigger garbage collection (always triggers in case of MRB_GC_STRESS), which in turn could reclaim the object we're trying to push to GC_ROOT_SYM.

@dearblue
Copy link
Contributor

A simple workaround is to reserve it before the critical section (not only for mruby).
An example using mrb_top_self(mrb) is shown below.

mrb_gc_register(mrb, mrb_top_self(mrb));    // RESERVED!

mrb_value need_protect = ...;
mrb_gc_unregister(mrb, mrb_top_self(mrb));  // RESTORE!
mrb_gc_register(mrb, need_protect);

However, this will keep squeezing memory if an exception occurs at the need_protect = ....
You may need to use mrb_protect_error() as well.

@matz matz closed this as completed in 3324773 Aug 1, 2024
@matz
Copy link
Member

matz commented Aug 1, 2024

I tried to address this issue. If it still causes problems, reopen this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants