From: "Sean O'Dell" Date: 2003-09-04T07:43:57+09:00 Subject: Re: Ruby GC eating my VALUE on the stack Mauricio Fern�ndez wrote: > On Thu, Sep 04, 2003 at 03:43:21AM +0900, Sean O'Dell wrote: > >>I thought I had read everything I needed about the Ruby GC, but >>something just popped up that took me by surprise. >> >>I am allocating a structure using malloc in QuiXML (in C), then wrapping >>it in Data_Wrap_Struct, providing mark and free functions. >> >>Data_Wrap_Struct returns a VALUE, which is stored in a variable on the >>stack. >> >>I never leave this function; I call other functions and pass the pointer >>to the structure around, but the variable holding the VALUE stays on the >>stack the entire time. >> >>I am stress-testing QuiXML, so I am deliberately dogging memory to get >>the GC to invoke naturally. >> >>At some point, a rb_str_new() call causes the GC to kick in because >>right then, the free callback for my structure gets called. >> >>Now, from what I understand, since the VALUE associated with the >>structure is still on the stack, the GC was supposed to see it and >>automatically mark it so my structure won't get freed prematurely. >> >>So why did it not get marked? Why was free called? > > > Is there any difference if you declare the VALUE to be volatile? That solved the problem! I guess because volatile forces the variable onto the stack, eh? Hmm...didn't think of that. Thanks Mauricio! Sean O'Dell