From: Eric Hodel Date: 2012-05-19T09:04:28+09:00 Subject: Re: rb_gc_register_address() or rb_gc_mark()? On May 18, 2012, at 00:20, Hans Mackowiak wrote: > Eric Hodel wrote in post #1061208: >> On May 17, 2012, at 12:31, Hans Mackowiak wrote: >>> i create an hash from the C-side, mark them with rb_global_variable and >>> then store my objects into them >>> >>> this protects my objects from being deleted. >> >> >> This is probably wrong. >> >> By using rb_global_variable the object can never be reclaimed by the >> garbage collector. >> >> You should either: >> >> Return a ruby object to the user that stores your Hash. >> >> Use Data_Wrap_Struct/Data_Make_Struct to store your Hash and use >> rb_gc_mark to indicate it is still alive. >> >> Use a constant, class or instance variable on a pre-defined class to >> store your Hash. > > no its correct i want the control when the GC gets the object or not. > thats why i use a hash, when my binding removes the object out of the > hash, GC deleteds it. but no earlier I was unclear, you should probably not use rb_global_variable on your Hash. You should instead bind your Hash as a regular ruby constant, wrap it in a struct, or return it as part of a regular ruby object.