From: "Sean O'Dell" Date: 2003-09-12T07:34:41+09:00 Subject: Re: GC - PLEASE set me straight Thomas Sondergaard wrote: >>Hmm ... I'm confused about that statement. > > > Well that wont do.. > > >>If the VALUE is a member of a class, then it's probably NOT on the >>stack, so volatile won't help you there. In this case, just use >>rb_gc_register_address/rb_gc_unregister_address in your >>constructor/destructor methods for your C++ class. > > > 1) I am using rb_gc_register/unregister in my C++ wrapper. > > 2) I have now added the volatile keyword in all the functions where I use an > automatic variable. > > > Here's the relevant bit of the C++ wrapper: > > public __gc class Object : public DynamicLanguageSupport::IObject { > public: > explicit Object(VALUE handle) { > _handle = ALLOC(VALUE); > *_handle = handle; > rb_gc_register_address(_handle); > } > > ~Object() { > rb_gc_unregister_address(_handle); > free(_handle); > } > > // stuff for invoking ruby methods from .net removed > > private: > VALUE __nogc *_handle; > }; > > Does it look reasonable? You don't need to ALLOC the VALUE though. You can just make it a regular member of your class and register the &address of it. When you get the &address of a member variable, it's true location in memory is given, and as far as I know that won't ever change during the lifetime of your object, so you can really just do it like this: public __gc class Object : public DynamicLanguageSupport::IObject { public: explicit Object(VALUE handle) { rb_gc_register_address(&_handle); } ~Object() { rb_gc_unregister_address(&_handle); } // stuff for invoking ruby methods from .net removed private: VALUE __nogc _handle; }; Aside from that, it sounds like you have got the knack of it! Sean O'Dell