On Tue, 08 Apr 2003 19:56:33 +0000, Steve Hart wrote:

> The scripts are loaded into a stack within my c++ code
> I have a c++ class 'GMScript' and have used swig to wrap into ruby
> I use a ruby script that inherits this class
> i.e.
> class Test1 < Gm::GMScript
> 
>         def initialize(sg,p1,p2)
> 
>                 super(sg,p1,p2)
> 
>                 @object1 = Gm::GMObjectNode.new("object1")
>         end
>         .
>         .
>         .
> end

I had some trouble with extending a SWIG-class, it resulted 
in a exception: nameerror. 
You are using inheiritance, no problems here.

 
> in c++ I call at start
> 
>         ruby_init();
>         ruby_init_loadpath();
>         ruby_script("embedded");
>         rb_require("gm.so");            - swigged up wrapper lib

Look fine.

 
> then each time a script load is requested i do the following (protect
> code removed for clarity)
> 
>         rb_load_protect(rb_str_new2("Test1.rb"),Qfalse,&state);
> 
> then
>         oClass = rb_const_get(rb_cObject, rb_intern("Test1"));
>         scriptObject = rb_funcall(oClass, rb_intern("new"), 
>                                   3,sgValue,pathName,className);
>         Data_Get_Struct(scriptObject,GMScript,scriptPtr);
>         scriptPtr->obj = scriptObject;

This code could be the problem. 
* bootstrapping of your swig class.
* you are not registering scriptObject, so GC won't know that its
  in usage and thus thinks it must get killed. 
  
How does your SWIG interface file look like ???
If it includes a .H file how does it look like ???


> The problem is that as I load more scripts previous scripts get freed
> of by the GC. In addition, the GMObjectNode's created within the
> Scripts are also freed. 

Sounds like you object is holding ruby-childen. 
You will need to do rb_mark on thise.


> What I want to do is only free off the Script
> when the user deletes it
> from the script stack and decide at this time if the GMObjectNode
> should
> also be deleted. I assumed that since the scriptObject is stored
> within the C code the GC would not free these off (or is it because
> the scriptObject is stored in GMScript superclass itself. 

Wrong assumption. It depends on wheter or not you have registered
your scriptObject.


> I cannot
> mark the object since this is under the control of Swig. I have tried
> 
>         rb_global_variable(&scriptObject)
> 
> but this does not prevent the freeing and in addition crashes the
> program with an error:

How did you "try" it ???


Read my text on embedding, look at the tar.gz file :-)
http://metaeditor.sourceforge.net/embed/

--
Simon Strandgaard