Ruby GC eating my VALUE on the stack

S

Sean O'Dell

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?

Sean O'Dell
 
M

Mauricio Fernández

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?

--
_ _
| |__ __ _| |_ ___ _ __ ___ __ _ _ __
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

The only other people who might benefit from Linux8086 would be owners
of PDP/11's and other roomsized computers from the same era.
-- Alan Cox
 
S

Sean O'Dell

Mauricio said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,797
Messages
2,569,647
Members
45,380
Latest member
LatonyaEde

Latest Threads

Top