"Simon Strandgaard" <0bz63fz3m1qt3001 / sneakemail.com> wrote in message news:<pan.2003.04.10.20.41.24.442608 / sneakemail.com>...
> 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.
> 
This got garbled in post - I just tried to follow through from
rb_funcall thru swig and back out again
>  
> > 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 ???

Sorry for all this I've had 2 days off and it's all a bit clearer. I
don't know why, but I had thought that the object instance (tObj)
returned from rb_funcall was also 'held' withing the interpreter. As
such, I assumed that the MARK would be called even though tObj had
gone out of scope.

I had some more thoughts on the tutorial (if you're interested). It
would make it clearer to seperate the swig and non swig version
completely (even sperate directories). People new to both swig and
ruby (like me) may find it easier to compare. Just a thought.

rubycpp.cpp is excellent though - I did'nt know you could obtain a
backtrace for error output
Cheers