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?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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?

rb_gc_mark() if I remember correctly.

Vale,
Marvin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJOD0h6AAoJELh1XLHFkqhatUEH/iYQUACmCFVHTng4qMz8o8eI
uWhaHPs67T99Ny948+KwxNKziSfbgnIY7es3tz6v7krIe7AwXo48NGI+ygf+IA1b
SBlal8yu7my5HaEczs93ah3qy2E/iJjkNlXWYD6vFFitM4tI9HHUumorwlgYI3US
ySxQtJc5FD1+ptakQWM959q+AnGqul0wMTlCFVd9V4FJDx4V8pY9obcbVKTwjpqS
u549tgqGoYGIkVpNJwcXNfSaVzZL+wNQFoyzUPDHjTIaEzqPT8Z6AE42Rhg8v4G4
jmO52BFaJYo7wu73X3jsoTSk+FBzjGDsfAqm6eLVU7K+3QfZZ8Mwv+VZlZXg3l0=
=LjnV
-----END PGP SIGNATURE-----

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.

— Kim Burgestrand (burgestrand.se)

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 [email protected]

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.

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

Maybe ruby is not suitable for embedding.
Hah…

s/Linux/Ruby: QDB: Quote #152037

Michael E.
[email protected]
http://carboni.ca/

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 [email protected]