From: Simon Strandgaard <0bz63fz3m1qt3001@...> Date: 2003-04-11T05:46:22+09:00 Subject: Re: embedding ruby with swig On Thu, 10 Apr 2003 10:10:54 +0000, Steve Hart wrote: > > My previous post has not appeared yet but I have a better solution > now. Forget the malloc - that was silly. Agree :-) [snip code] > which makes sense - each subsequent rb_funcall creates a new object > and the number of marks increase accordingly. But if I omit the call > to > > rb_gc_register_address(&(ptr->scriptObj)); > > I get: > > GMScript::GMScript > > FREE > GMScript::~GMScript > GMScript::GMScript > > FREE > GMScript::~GMScript > GMScript::GMScript > > The mark function in swig is never called. Why?. If you omit telling GC that the instance is busy. Then when GC is done with its mark phase and starts sweeping, then ptr->scriptObj will appear to be unmark and get destroyed. Thats the reason why ptr->scriptObj's markfunc is never invoked. > If I follow the code through > rb_funcall --> _wrap_GMScript_allocate --> SWIG_NewClassInstance --> > Data_Wrap.. > --> rb_obj_call_init --> > initialize() > self <--- Sorry.. me don't understand. > So, does this mean that the only reference held to this ruby instance > is the one returned from rb_funcall and if I don't tell gc it can't > call the mark. And the only reason the free can be called is because > it exists in main's stack frame? Sorry.. again not understanding. > Sorry for being a bit anal here, I'm just trying to get my head round > what's going on. > If the above is true, then great. I would be happy to call > rb_gc_register_address when I create the instance, use the swig mark > func for any persistant children i create within MyClass, and then > call rb_gc_unregister_address when I'm done with it. I think I read Yes you got it right :-) > somewhere that there is no harm in marking an object twice so if child > objects created within GMScript are shared with other instances of > GMScript and they both mark the chhildren I can select which ones I > need to delete when I destroy an instance to GMScript. > > Am I correct here? Marking an instance multiple times.. I don't think it will result in segfaults :-) > I did read the Swig doc on this - could do with a bit more explaining > here, in particular sec 20.8.5 which refers to the imaginary > SWIG_RubyInstanceFor. any chance anyone might implement it! Don't know ??? -- Simon Strandgaard