From: "José Augusto" Date: 2003-04-05T00:00:23+09:00 Subject: Re: Strange behavior in C-declared global variable? Hi Thanks very much for your reply. You are right and declaring temp as static solves the problem, as well as using rb_iv_set. The code you suggested below works perfectly! I examined the interrelation of rb_gv_set and rb_define_variable in the Ruby code (file variable.c) and posted my conclusions in other post (replying to Kent Dahls' answer), so I dont repeat them here. Thanks J. Augusto ts wrote: > >>>>> "J" == =?iso-8859-1?Q?Jos=E9?= Augusto writes: > > J> static VALUE newvar(int argc, VALUE *argv, VALUE self) { > J> VALUE temp; > > J> temp = rb_ary_new(); > J> rb_ary_push(temp, rb_float_new(3.3)); > J> rb_define_variable("$tempor", &temp); > J> rb_global_variable(&temp); > J> return Qnil; > J> } > > Don't do this rb_define_variable() and rb_global_variable() must be used > *only* with "global" variables, like for example in > > /* see the declaration of temp0 and temp1 outside the function */ > static VALUE temp0, temp1; > > void Init_tt() > { > temp0 = rb_ary_new(); > temp1 = rb_ary_new(); > /* > this variable can be accessed from ruby and C > */ > rb_define_variable("$tempor", &temp0); > /* > this variable can be accessed only from C > this is to protect it against the GC > */ > rb_global_variable(&temp1); > } > > In your case you want rb_gv_set(), rb_gv_get() (see p. 197 of the pickaxe) > > Something like this (not tested) > > static VALUE newvar(int argc, VALUE *argv, VALUE self) { > VALUE temp; > > temp = rb_ary_new(); > rb_ary_push(temp, rb_float_new(3.3)); > rb_gv_set("$tempor", temp); > return Qnil; > } > > Guy Decoux