Use RB_GC_GUARD to prevent GC from freeing String copies used by iterators #191

wants to merge 1 commit into


None yet

2 participants


This fixes the bug which I reported yesterday at:

I was having a problem with incorrect values yielded by String#codepoints, because the frozen string copy which it uses internally was being freed by the GC. I noticed that very similar code is used in String#lines and String#chars, so I added RB_GC_GUARD to those also to make sure that similar problems do not happen to anyone when using those methods.


Sorry, one more thing: this bug occurs on both Ruby 1.9.2 and 1.9.3. I have confirmed that this patch can be applied cleanly to both v1_9_2_381 and v1_9_3_195. I recommend that you cherry-pick it onto both the 1.9.2 and 1.9.3 maintenance branches (so it will be included in future bugfix releases).

Ruby Programming Language member

RB_GC_GUARD() before RSTRING_PTR/LEN can be optimized out also.

@nobu nobu closed this Oct 11, 2012

OK... let me try again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment