Ruby C API:How to prevent a VALUE being collected by the garbage collecter?

Ruby C API:How to prevent a VALUE being collected by the garbage collecter?
Is there any function to call?

rb_gc_mark() if I remember correctly.

Vale,
Marvin

···

Am 02.07.2011 17:34, schrieb ZaCk:

Ruby C API:How to prevent a VALUE being collected by the garbage
collecter? Is there any function to call?

If you want it to never be collected, make it a global variable. rb_gc_mark will
only do what you want until the next GC cycle.

— Kim Burgestrand (burgestrand.se)

···

On Saturday, 2 July 2011 at 18:35, Quintus wrote:

> Ruby C API:How to prevent a VALUE being collected by the garbage
> collecter? Is there any function to call?

Ruby C API:How to prevent a VALUE being collected by the garbage
collecter? Is there any function to call?

If you want it to never be collected, make it a global variable. rb_gc_mark will
only do what you want until the next GC cycle.

That's right. Take a look at README.EXT in ruby source, which says:

GC should know about global variables which refer to Ruby's objects, but
are not exported to the Ruby world. You need to protect them by

  void rb_global_variable(VALUE *var)

So if you do this:

VALUE my_global;
...
   rb_global_variable(&my_global);

Then whatever object is referenced by my_global when GC runs will not be collected.

If you just want to protect some VALUEs that are not necessarily pointed to by C variables, you could just keep them in a (ruby) array or other data structure, as long as that array is reachable from some variable known to ruby.

···

On 07/02/2011 11:35 AM, Kim Burgestrand wrote:

On Saturday, 2 July 2011 at 18:35, Quintus wrote:

Thanks.
Finally I use rb_gc_mark to do this. I am embedding ruby, but the program
collapsed when calling rb_funcall. I debugged it and found out that if GC
occurs during the calling, the arguments would be collected, and the program
throwed a segment fault. I tried rb_gc_mark and rb_gc_force_recycle to
protect the arguments. It worked. Maybe ruby is not suitable for embedding.
Hah...

···

2011/7/3 Joel VanderWerf <joelvanderwerf@gmail.com>

On 07/02/2011 11:35 AM, Kim Burgestrand wrote:

On Saturday, 2 July 2011 at 18:35, Quintus wrote:

Ruby C API:How to prevent a VALUE being collected by the garbage

collecter? Is there any function to call?

If you want it to never be collected, make it a global variable.

rb_gc_mark will
only do what you want until the next GC cycle.

That's right. Take a look at README.EXT in ruby source, which says:

GC should know about global variables which refer to Ruby's objects, but

are not exported to the Ruby world. You need to protect them by

void rb_global_variable(VALUE *var)

So if you do this:

VALUE my_global;
...
rb_global_variable(&my_global)**;

Then whatever object is referenced by my_global when GC runs will not be
collected.

If you just want to protect some VALUEs that are not necessarily pointed to
by C variables, you could just keep them in a (ruby) array or other data
structure, as long as that array is reachable from some variable known to
ruby.

Thanks.
Finally I use rb_gc_mark to do this. I am embedding ruby, but the program
collapsed when calling rb_funcall. I debugged it and found out that if GC
occurs during the calling, the arguments would be collected, and the program
throwed a segment fault. I tried rb_gc_mark and rb_gc_force_recycle to
protect the arguments. It worked. Maybe ruby is not suitable for embedding.
Hah...

···

2011/7/3 ZaCk <ladacez@gmail.com>

Thanks.
Finally I use rb_gc_mark to do this. I am embedding ruby, but the program
collapsed when calling rb_funcall. I debugged it and found out that if GC
occurs during the calling, the arguments would be collected, and the program
throwed a segment fault. I tried rb_gc_mark and rb_gc_force_recycle to
protect the arguments. It worked. Maybe ruby is not suitable for embedding.
Hah...

2011/7/3 Joel VanderWerf <joelvanderwerf@gmail.com>

On 07/02/2011 11:35 AM, Kim Burgestrand wrote:

On Saturday, 2 July 2011 at 18:35, Quintus wrote:

Ruby C API:How to prevent a VALUE being collected by the garbage

collecter? Is there any function to call?

If you want it to never be collected, make it a global variable.

rb_gc_mark will
only do what you want until the next GC cycle.

That's right. Take a look at README.EXT in ruby source, which says:

GC should know about global variables which refer to Ruby's objects, but

are not exported to the Ruby world. You need to protect them by

void rb_global_variable(VALUE *var)

So if you do this:

VALUE my_global;
...
rb_global_variable(&my_global)**;

Then whatever object is referenced by my_global when GC runs will not be
collected.

If you just want to protect some VALUEs that are not necessarily pointed
to by C variables, you could just keep them in a (ruby) array or other data
structure, as long as that array is reachable from some variable known to
ruby.

s/Linux/Ruby: http://bash.org/?152037

Michael Edgar
adgar@carboni.ca
http://carboni.ca/

···

On Jul 2, 2011, at 9:16 PM, ZaCk wrote:

Maybe ruby is not suitable for embedding.
Hah...