From: Rolando Abarca Date: 2009-05-07T05:59:47+09:00 Subject: storing c pointers in ruby hash Hi all, I'm been banging my head on the keyboard the whole day trying to track a bug. I have a embedded ruby (branch 1_9_1, checked out today) running on a Objective-C project (if anyone's interested, it's this project: http://github.com/funkaster/shinycocos/tree/master) . The idea is to keep a reference to the ruby counterpart of the Obj-C object. Right now, I'm using a "global" hash table, defined as this: VALUE sc_object_hash = rb_hash_new(); The variable has been registered to the GC as suggested in the README.EXT: rb_global_variable(&sc_object_hash); An objc object is associated to a new ruby object (the ruby version of the objc class) this way: node = [[SomeClass alloc] init]; VALUE rb_obj = common_init(klass, nil, node, argc, argv, YES); common_link(node, rb_obj); common_init is a function defined as follows: VALUE common_init(VALUE klass, cocos_holder **ret_ptr, id object, int argc, VALUE *argv, BOOL release_on_free) { VALUE obj; cocos_holder *ptr; if (release_on_free) obj = Data_Make_Struct(klass, cocos_holder, 0, common_free, ptr); else obj = Data_Make_Struct(klass, cocos_holder, 0, common_free_no_release, ptr); ptr->_obj = object; rb_obj_call_init(obj, argc, argv); if (ret_ptr != nil) *ret_ptr = ptr; return obj; } and common_link is an inline function: static inline void common_link(id obj1, VALUE obj2) { rb_hash_aset(sc_object_hash, INT2FIX((long)obj1), obj2); } Some times, the objc framework will call a method in the objc object. When this happens, I need to call the proper ruby method in the corresponding ruby object: - (void)someMethodCalledByTheFramework { VALUE rb_obj = common_link_ref(self); rb_funcall(rb_obj, id_cb_on_enter, 0, 0); } common_link_ref is the counterpart of common_link (basically calls rb_hash_aref on the same hash) The thing is, that the rb_obj is crashing the ruby interpreter. If I try to call any method on it (even inspect) I get a bad access error. It was working with a ruby 1.8 version, but now that I've ruby 1.9, it refuses to work... Any idea of what I could be doing wrong here? Is there a better way to link a ruby object with a c pointer? I need to get the associated ruby object to a given objc object. Not the other way around (which is easy using Data_Get_Struct)... that's why I'm using a hash table. thanks! -- Rolando Abarca M.