I hope I understood you.....

ruby's GC uses stack and register as root, and trace reference from root and marks them.
Finally, frees unmarked object. The problem is, sometimes compiler removes VALUE
>from stack and register despite it's still in scope. 
If all refrences are removed like that, corresponding object are freed wrongly. right?

> In your example, you constantly work with an object `v' and not the content
> of the object, like RARRAY(v)->ptr. This mean that this object `v' will be
> in a register or in the stack (ruby need it) and it will marked by the GC

But, there is possibility of inline expansion. If rb_fooboo and func2 and func3
are simpile enough, they can be all expanded to somefunc() like this.

  void somefunc()
  {
      VALUE s = rb_str_new2("foo");

      /* too complex codes here */

      /* came from rb_fooboo */
      if (RSTRING(s)->len > 343)
      {
          syscall(RSTRING(s)->ptr);

          ....
      }

      if (/* ...... */)
      {
         /* came from func2 */

         int i = -1 * 2 + 3 + RSTRING(s)->len;

         .....
      }
      else
      {
         /* came from func3 */

         ......
      }
  }

And if s is optimized out. all references to ruby string "foo" disappers,
and GC may destroy the object. If s is volatile, there is not any chance
to erase all references because one of them is volatile. (Am I wrong?)
And if we have to keep only one refernce to stack or register, is there better
place to do it than after object creation?

Compiler can do anything(inlining, erasing loop variable, reordering of code...) and
I feel it's hard to expect such compiler's behavior.

Anyway, I was told gcc -O3 or more shouldn't be used
because too much optimization will break GC. Doesn't that mean protection is not enough?

# Sorry, this comes from curiousty. I just want to know about what I'm doing.....