# Rb\_gc() and scan stack

**URL:** <https://rubytalk.org/t/rb-gc-and-scan-stack/6660>\
**Category:** ruby-talk\
**Created:** [1 July 2003 22:52 UTC](https://rubytalk.org/t/rb-gc-and-scan-stack/6660 "2003-07-01T22:52:17Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Simon\_Strandgaard](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@Simon\_Strandgaard](https://rubytalk.org/u/Simon_Strandgaard)\
**Post date:** [1 July 2003 22:52 UTC](https://rubytalk.org/t/rb-gc-and-scan-stack/6660/1 "2003-07-01T22:52:17Z")

</div>

I were experimenting with Init\_stack, when I discovered a flaw in my mind.

I thought that: local variables on stack would be marked in the  
mark-phase and therefore survive the sweep-phase.

But my instance is being destroyed… why ?

Can someone enligthen me ?

> expand -t4 rb\_gc1.c  
> #include \<ruby.h\>

void Dummy\_Free(void _p) {  
printf(“free\n”);  
delete [] ((int_)p);  
}

VALUE Dummy\_Alloc(VALUE self) {  
printf(“alloc\n”);  
int \*p = new int[1];  
return Data\_Wrap\_Struct(self, 0, Dummy\_Free, p);  
}

VALUE Dummy\_Initialize(VALUE self) {  
int \*p;  
printf(“initialize\n”);  
Data\_Get\_Struct(self, int, p);  
return self;  
}

void Init\_dummy() {  
VALUE h = rb\_define\_class(“Dummy”, rb\_cObject);  
rb\_define\_alloc\_func(h, Dummy\_Alloc);  
typedef VALUE (\*HOOK)(…);  
rb\_define\_method(h, “initialize”, (HOOK)(&Dummy\_Initialize), 0);  
}

VALUE tst\_new\_dummy() {  
return rb\_class\_new\_instance(0, 0, rb\_path2class(“Dummy”));  
}

void test() {  
VALUE instance = tst\_new\_dummy();  
printf(“1\n”);  
rb\_p(instance);  
// the instance SHOULD survive this step (1\_2)  
rb\_gc();  
printf(“2\n”);  
rb\_p(instance);  
// the instance should NOT survive this step (2\_3)  
instance = Qnil;  
rb\_gc();  
printf(“3\n”);  
rb\_p(instance);  
}

int main(int argc, char \*\*argv) {  
ruby\_init();  
Init\_dummy();  
test();  
return 0;  
}

> 

> ./a.out  
> alloc  
> initialize  
> 1  
> #Dummy:0x8103b38  
> free  
> 2  
> ruby: [BUG] Segmentation fault  
> ruby 1.8.0 (2003-06-23) [i386-freebsd5.0]

Abort (core dumped)

> 

As you can see the ‘free’ is occuring between ‘1’ and ‘2’.

I thought ‘free’ was suppose to happen between ‘2’ and ‘3’ ?  
Expecting output should look like this:

alloc  
initialize  
1  
#Dummy:0x8103b38  
2  
#Dummy:0x8103b38  
free  
3  
ruby: [BUG] Segmentation fault  
ruby 1.8.0 (2003-06-23) [i386-freebsd5.0]

Abort (core dumped)

> **···**
>
> –  
> Simon Strandgaard

---

<div class="post-metadata">

**Author:** ![Simon\_Strandgaard](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@Simon\_Strandgaard](https://rubytalk.org/u/Simon_Strandgaard)\
**Post date:** [1 July 2003 23:13 UTC](https://rubytalk.org/t/rb-gc-and-scan-stack/6660/2 "2003-07-01T23:13:00Z")

</div>

> I were experimenting with Init\_stack, when I discovered a flaw in my mind.
> 
> I thought that: local variables on stack would be marked in the  
> mark-phase and therefore survive the sweep-phase.
> 
> But my instance is being destroyed… why ?
> 
> Can someone enligthen me ?

[snip]

> As you can see the ‘free’ is occuring between ‘1’ and ‘2’.
> 
> I thought ‘free’ was suppose to happen between ‘2’ and ‘3’ ?  
> Expecting output should look like this:

Init\_stack() did solve it :-)… The funny thing is that I were looking  
for a short+simple example to illustrate usage of Init\_stack().

> **[WebMD - Better information. Better health.](https://www.webmd.com/?RubyApi%2FInitStack)**
>
> The leading source for trustworthy and timely health and medical news and information. Providing credible health information, supportive community, and educational services by blending award-winning expertise in content, community services, expert...

The modification I made were this:

```
extern "C" void Init_stack(void *); 
void test() {
    volatile void *tmp;
    Init_stack((void*)&tmp); // comment this line and then step 1_2 will fail.

```

Im _still_ confused about the rb\_gc\_stack\_start value in my first posting.  
Why does it fail in step (1\_2), Can someone enligthen me ???

> **···**
>
> On Wed, 02 Jul 2003 01:37:11 +0200, Simon Strandgaard wrote:
> 
> –  
> Simon Strandgaard

---

<div class="post-metadata">

**Author:** ![Mauricio\_Fernndez](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@Mauricio\_Fernndez](https://rubytalk.org/u/Mauricio_Fernndez)\
**Post date:** [2 July 2003 12:17 UTC](https://rubytalk.org/t/rb-gc-and-scan-stack/6660/3 "2003-07-02T12:17:41Z")

</div>

> I were experimenting with Init\_stack, when I discovered a flaw in my mind.
> 
> I thought that: local variables on stack would be marked in the  
> mark-phase and therefore survive the sweep-phase.
> 
> But my instance is being destroyed… why ?
> 
> Can someone enligthen me ?  
> […]  
> int main(int argc, char \*\*argv) {  
> ruby\_init();  
> Init\_dummy();  
> test();  
> return 0;  
> }

Could you please check if adding one stack frame in between works?  
If it does, it’d mean it’s exactly the same issue described in the links  
I provided some time ago, that is, that ruby\_init is capturing the  
current SP to record the beginning of the stack, but you’re storing  
VALUEs in a stack frame above.

Please test adding the following to your code:

void foo()  
{  
int a, b, c, d, e, f; /\* just to take place in the stack _/  
a = b = c = d = e = f = 1; /_ don’t get warnings when -Wall \*/  
test();  
}

int main(int argc, char \*\*argv)  
{  
ruby\_init();  
Init\_dummy();  
foo();  
return 0;  
}

Cannot check now as my primary machine has no connection and the one  
I’m writing from has no ruby…

> **···**
>
> On Wed, Jul 02, 2003 at 07:52:17AM +0900, Simon Strandgaard wrote:
> 
> –  
> \_ \_
> 
> > > \_\_ \_\_ _| |_ \_\_\_ \_ \_\_ \_\_\_ \_\_ \_ \_ \_\_  
> > > '\_ \ / _`| __/__ | '_` \_ \ / ` | ’ \   
> > > ) | (| | |_\_\_ \ | | | | | (_| | | | |  
> > > .\_\_/ \_,_|\ __|__ /| |_| |_|\__,_|_| |\_|  
> > > Running Debian GNU/Linux Sid (unstable)  
> > > batsman dot geo at yahoo dot com
> 
> Those who don’t understand Linux are doomed to reinvent it, poorly.  
> – unidentified source

---

<div class="post-metadata">

**Author:** ![Simon\_Strandgaard](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@Simon\_Strandgaard](https://rubytalk.org/u/Simon_Strandgaard)\
**Post date:** [2 July 2003 09:16 UTC](https://rubytalk.org/t/rb-gc-and-scan-stack/6660/4 "2003-07-02T09:16:27Z")

</div>

Guy Decoux + others… I know you are reading this… please help 🙂

> **···**
>
> On Wed, 02 Jul 2003 02:14:23 +0200, Simon Strandgaard wrote:
> 
> > Im _still_ confused about the rb\_gc\_stack\_start value in my first posting.  
> > Why does it fail in step (1\_2), Can someone enligthen me ???
> 
> –  
> Simon Strandgaard

---

<div class="post-metadata">

**Author:** ![Simon\_Strandgaard](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@Simon\_Strandgaard](https://rubytalk.org/u/Simon_Strandgaard)\
**Post date:** [2 July 2003 13:18 UTC](https://rubytalk.org/t/rb-gc-and-scan-stack/6660/5 "2003-07-02T13:18:15Z")

</div>

> > I were experimenting with Init\_stack, when I discovered a flaw in my mind.
> > 
> > I thought that: local variables on stack would be marked in the  
> > mark-phase and therefore survive the sweep-phase.
> > 
> > But my instance is being destroyed… why ?
> > 
> > Can someone enligthen me ?  
> > […]  
> > int main(int argc, char \*\*argv) {  
> > ruby\_init();  
> > Init\_dummy();  
> > test();  
> > return 0;  
> > }
> 
> Could you please check if adding one stack frame in between works?  
> If it does, it’d mean it’s exactly the same issue described in the links  
> I provided some time ago, that is, that ruby\_init is capturing the  
> current SP to record the beginning of the stack, but you’re storing  
> VALUEs in a stack frame above.
> 
> Please test adding the following to your code:  
> [snip]
> 
> Cannot check now as my primary machine has no connection and the one  
> I’m writing from has no ruby…

It doesn’t help… my main now looks like this:

> expand -t4 main.c | tail -n12  
> void foo() {  
> int a, b, c, d, e, f; /\* just to take place in the stack _/  
> a = b = c = d = e = f = 1; /_ don’t get warnings when -Wall \*/  
> test();  
> }

int main(int argc, char \*\*argv) {  
ruby\_init();  
Init\_dummy();  
foo();  
return 0;  
}

> 

Output is the same:

> ./a.out  
> alloc  
> initialize  
> 1  
> #Dummy:0x8103b38  
> free  
> 2  
> ruby: [BUG] Segmentation fault  
> ruby 1.8.0 (2003-06-23) [i386-freebsd5.0]

Abort (core dumped)

> 

I tried drawing on some paper, what is happening…  
My initial drawing were wrong… Now I understand.  
No problemo:

The stack grows againts right.

‘.’ = empty stack range (unkown to GC).  
‘R’ = stack range scanned by GC.  
left-most-R == rb\_gc\_stack\_end.  
right-most-R == rb\_gc\_stack\_begin (set by Init\_stack).  
‘|’ = current stack pointer.  
‘i’ = where the instance is on the stack.

–[0]-----------------  
top/hi […] bottom/lo  
\>

–[1]-----------------  
entering ruby\_init()  
top/hi […] bottom/lo  
\>

–[2]-----------------  
leaving ruby\_init()  
top/hi [RRRRR…] bottom/lo  
\>

–[3]-----------------  
entering Init\_dummy()  
top/hi [RRRRR…] bottom/lo  
\>

–[4]-----------------  
leaving Init\_dummy()  
top/hi [RRRRR…] bottom/lo  
\>

–[5]-----------------  
entering test()  
top/hi [RRRRR…] bottom/lo  
\>

–[6]-----------------  
instance = Dummy.new  
The instance is not within the mark-area, therefore it dies!  
top/hi [RRRRR…i…] bottom/lo  
\>

–[7]-----------------  
rb\_gc(); /\* first time \*/

The instance is not within the RRRRR range… and  
Ruby is therefore unable to mark the instance.  
Thus it dies!

top/hi [RRRRR…] bottom/lo  
\>

If we redo from step6 with Init\_stack(begin)… with a  
lower begin address, then things will work.

–[6a]-----------------  
Init\_stack(current\_stack\_ptr)  
top/hi [RRRRRRRR…] bottom/lo  
\>

–[7a]-----------------  
instance = Dummy.new  
This time the instance is within the area, and will thus survive  
top/hi [RRRRRRRR…] bottom/lo  
\>

–[8a]-----------------  
rb\_gc(); /\* first time: this time we survie \*/  
top/hi [RRRRRRRR…] bottom/lo  
\>

–[9a]-----------------  
i = nil  
top/hi [RRRRRRRR…] bottom/lo  
\>

–[10a]-----------------  
rb\_gc(); /\* second time: this time we die \*/  
top/hi [RRRRRRRR…] bottom/lo  
\>

–[11a]-----------------  
leaving test();  
top/hi [RRRRRRRR…] bottom/lo

> **···**
>
> On Wed, 02 Jul 2003 22:17:41 +0900, Mauricio Fernández wrote:
> 
> > On Wed, Jul 02, 2003 at 07:52:17AM +0900, Simon Strandgaard wrote:
> 
> –  
> Simon Strandgaard

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [2 July 2003 12:27 UTC](https://rubytalk.org/t/rb-gc-and-scan-stack/6660/6 "2003-07-02T12:27:28Z")

</div>

Hi,

> **···**
>
> In message “Re: rb\_gc() and scan stack” on 03/07/02, “Simon Strandgaard” [0bz63fz3m1qt3001@sneakemail.com](mailto:0bz63fz3m1qt3001@sneakemail.com) writes:
> 
> > > Im _still_ confused about the rb\_gc\_stack\_start value in my first posting.  
> > > Why does it fail in step (1\_2), Can someone enligthen me ???
> > 
> > Guy Decoux + others… I know you are reading this… please help 🙂
> 
> We can’t say much. As far as I guess stack, position was higher than  
> the position where Init\_stack() was called from ruby\_init().
> 
> ```
> matz.
> 
> ```

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nobuyoshi\_Nakada](https://rubytalk.org/u/Nobuyoshi_Nakada)\
**Post date:** [2 July 2003 13:43 UTC](https://rubytalk.org/t/rb-gc-and-scan-stack/6660/7 "2003-07-02T13:43:48Z")

</div>

Hi,

> I tried drawing on some paper, what is happening…  
> My initial drawing were wrong… Now I understand.  
> No problemo:

Incorrect.

G == rb\_gc\_stack\_start (set by Init\_stack).

–[0]-----------------  
bottom […] top  
\>

–[1]-----------------  
entering ruby\_init()  
bottom […G…] top  
\>

–[2]-----------------  
leaving ruby\_init()  
bottom [.RRRRRRG…] top  
\>

–[3]-----------------  
entering Init\_dummy()  
bottom […RRRRRG…] top  
\>

–[4]-----------------  
leaving Init\_dummy()  
bottom [.RRRRRRG…] top  
\>

–[5]-----------------  
entering test()  
bottom […RG…] top  
\>

–[6]-----------------  
instance = Dummy.new  
The instance is not within the mark-area, therefore it dies!  
bottom […i.RG…] top  
\>

> If we redo from step6 with Init\_stack(begin)… with a  
> lower begin address, then things will work.

–[6a]-----------------  
Init\_stack(current\_stack\_ptr)  
bottom […G…] top  
\>

–[7a]-----------------  
instance = Dummy.new  
This time the instance is within the area, and will thus survive  
bottom […GRiR…] top

> **···**
>
> At Wed, 2 Jul 2003 22:18:15 +0900, Simon Strandgaard wrote:  
> \>
> 
> –  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Simon\_Strandgaard](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@Simon\_Strandgaard](https://rubytalk.org/u/Simon_Strandgaard)\
**Post date:** [2 July 2003 13:18 UTC](https://rubytalk.org/t/rb-gc-and-scan-stack/6660/8 "2003-07-02T13:18:30Z")

</div>

OK… I had a flaw on my initial drawing… I understand now.  
See my revisited drawings of the stack, here:  
[http://ruby-talk.org/74950](http://ruby-talk.org/74950)

I think I am ready to add this as an example of Init\_stack, here:

> **[WebMD - Better information. Better health.](https://www.webmd.com/?RubyApi%2FInitStack)**
>
> The leading source for trustworthy and timely health and medical news and information. Providing credible health information, supportive community, and educational services by blending award-winning expertise in content, community services, expert...

Thanks everyone who looked at it 🙂

> **···**
>
> On Wed, 02 Jul 2003 22:27:28 +0900, Yukihiro Matsumoto wrote:
> 
> > Hi,
> > 
> > In message “Re: rb\_gc() and scan stack” \> on 03/07/02, “Simon Strandgaard” [0bz63fz3m1qt3001@sneakemail.com](mailto:0bz63fz3m1qt3001@sneakemail.com) writes:
> > 
> > > > Im _still_ confused about the rb\_gc\_stack\_start value in my first posting.  
> > > > Why does it fail in step (1\_2), Can someone enligthen me ???
> > > 
> > > Guy Decoux + others… I know you are reading this… please help 🙂
> > 
> > We can’t say much. As far as I guess stack, position was higher than  
> > the position where Init\_stack() was called from ruby\_init().
> > 
> > ```
> > matz.
> > 
> > ```
> 
> –  
> Simon Strandgaard

---

<div class="post-metadata">

**Author:** ![Simon\_Strandgaard](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@Simon\_Strandgaard](https://rubytalk.org/u/Simon_Strandgaard)\
**Post date:** [2 July 2003 16:00 UTC](https://rubytalk.org/t/rb-gc-and-scan-stack/6660/9 "2003-07-02T16:00:35Z")

</div>

I have some major/minor problems understanding your diagram.

What confuses me is that ‘G’ is right-most in step 1-6.  
And that ‘G’ is left-most in step 7a.  
This seems odd to me, Is this really correct behavier ?

I should have mentionen that I am talking about a x86 stack  
which grows downward. In my posting the stack therefore had  
its high-addeses to the left… and the lower addesses at right.  
I see you have swapped ‘bottom’ with ‘top’… this confuses me.  
Are you refering to a x86 stack or a motorola stack ?

Thanks Nobu for the otherwise nice diagram 🙂

> **···**
>
> On Wed, 02 Jul 2003 23:43:48 +0900, nobu.nokad wrote:
> 
> > At Wed, 2 Jul 2003 22:18:15 +0900, \> Simon Strandgaard wrote:
> > 
> > > I tried drawing on some paper, what is happening…  
> > > My initial drawing were wrong… Now I understand.  
> > > No problemo:
> > 
> > Incorrect.
> 
> –  
> Simon Strandgaard

---

<div class="post-metadata">

**Author:** ![Simon\_Strandgaard](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@Simon\_Strandgaard](https://rubytalk.org/u/Simon_Strandgaard)\
**Post date:** [2 July 2003 13:58 UTC](https://rubytalk.org/t/rb-gc-and-scan-stack/6660/10 "2003-07-02T13:58:43Z")

</div>

Wrong url… instead  
[http://ruby-talk.org/74951](http://ruby-talk.org/74951)

Its a nice drawing of what is actually occuring + snapshots  
of the stack 🙂

> **···**
>
> On Wed, 02 Jul 2003 16:09:07 +0200, Simon Strandgaard wrote:
> 
> > OK… I had a flaw on my initial drawing… I understand now.  
> > See my revisited drawings of the stack, here:  
> > [http://ruby-talk.org/74950](http://ruby-talk.org/74950)
> 
> –  
> Simon Strandgaard

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nobuyoshi\_Nakada](https://rubytalk.org/u/Nobuyoshi_Nakada)\
**Post date:** [2 July 2003 17:48 UTC](https://rubytalk.org/t/rb-gc-and-scan-stack/6660/11 "2003-07-02T17:48:41Z")

</div>

Hi,

> What confuses me is that ‘G’ is right-most in step 1-6.  
> And that ‘G’ is left-most in step 7a.  
> This seems odd to me, Is this really correct behavier ?

It _was_ correct. To be accurate, nothing shouldn’t be marked  
in step 2 through 6. But rb\_gc\_mark\_locations(), which marks  
stack, swaps its arguments when they are out of order. So  
garbages would possibly be marked.

# I forgot to remove this now useless code.

> I should have mentionen that I am talking about a x86 stack  
> which grows downward. In my posting the stack therefore had  
> its high-addeses to the left.. and the lower addesses at right.  
> I see you have swapped ‘bottom’ with ‘top’.. this confuses me.  
> Are you refering to a x86 stack or a motorola stack ?

Both x86 and mc68000 stacks grow downward. IIRC, beginning of  
stack is called “bottom”, and the edge where an element is  
pushed into and popped from is called “top”.

> **···**
>
> At Thu, 3 Jul 2003 01:00:35 +0900, Simon Strandgaard wrote:
> 
> –  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Simon\_Strandgaard](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@Simon\_Strandgaard](https://rubytalk.org/u/Simon_Strandgaard)\
**Post date:** [8 July 2003 21:05 UTC](https://rubytalk.org/t/rb-gc-and-scan-stack/6660/12 "2003-07-08T21:05:54Z")

</div>

“swaps its arguments”… I oversaw this.  
About 5 minuts I just realized this, browsing ‘gc.c’ 🙂

Thanks Nobu.

> **···**
>
> On Thu, 03 Jul 2003 03:48:41 +0900, nobu.nokad wrote:
> 
> > At Thu, 3 Jul 2003 01:00:35 +0900, Simon Strandgaard wrote:
> > 
> > > What confuses me is that ‘G’ is right-most in step 1-6.  
> > > And that ‘G’ is left-most in step 7a.  
> > > This seems odd to me, Is this really correct behavier ?
> > 
> > It _was_ correct. To be accurate, nothing shouldn’t be marked  
> > in step 2 through 6. But rb\_gc\_mark\_locations(), which marks  
> > stack, swaps its arguments when they are out of order. So  
> > garbages would possibly be marked.
> 
> –  
> Simon Strandgaard
