Hi,

At Tue, 24 Jun 2003 16:03:29 +0900,
Simon Strandgaard wrote:
> > To be more specific, I am on WIN XP Pro, using VC++ 6.0
> > The following code does not work (it crashes and asks me if I
> > want to send the error report to Microsoft. Probably because I
> > do not know how "... to tell the Ruby's garbage collector that
> > there is a new instance in town" )
> 
> There is 2 ways you can inform GC about the new instance.
> #1   rb_define_variable(name, instance);
>      by naming the instance, so it can be accessed by its
>      Ruby-variable-name. Use this if you want to share an
>      instance between Ruby and you extension/embedding code.
> 
> #2   rb_gc_register_address(&instance);
>      don't want to assign a name to the instance.
>      Use this if you want to keep some variables *private* to
>      your extension/embedding code. 
>      It cannot be accessed from a ruby-script.

  #3   Init_stack(address)
       tell GC the machine stack limit.

> #include <ruby.h>
> int main(int argc, char *argv[]) {
> 	int n;
> 	VALUE args[2];
> 	VALUE klass;
> 	VALUE instance;
> 
> 	ruby_init();
	Init_stack(&argc);	/* arbitrary higher address */

> 	args[0] = INT2FIX(4);
> 	args[1] = rb_str_new2("ok");
> 	n = 2;
> 	klass = rb_path2class("Array");
> 	instance = rb_class_new_instance(n, args, klass);

#if 0	/* no need */
> 	/* tell GC that there is a new instance in town. */
> 	rb_gc_register_address(&instance);
#endif

> 	rb_p(instance);
> 
> 	ruby_finalize();
> 	return 0;
> } 

Better solution is separating from "args[0] = ..." through
ruby_finalize() to another function.

-- 
Nobu Nakada