From: ts Date: 2002-04-20T00:39:07+09:00 Subject: Re: GC problem in C++ extension >>>>> "P" == Paul Brannan writes: P> Hmm... if I change line 327 of gc.c from: P> VALUE obj; P> to: P> static VALUE obj; P> then my program works. OK here your example : pigeon% cat uu.cc #include #include #include #include #if defined(RUBY_METHOD_FUNC) #undef RUBY_METHOD_FUNC #endif typedef VALUE (*RUBY_METHOD_FUNC)(); class Foo { public: Foo(char const * s) : s_(s) { } ~Foo() { std::cout << s_ << std::endl; } static void free(Foo * f) { delete f; } private: std::string s_; }; VALUE test_foo(VALUE) { Foo * f = new Foo("testing..."); VALUE v = Data_Wrap_Struct(rb_cObject, 0, (RUBY_DATA_FUNC)Foo::free, f); return Qnil; // line 25 } int main(int argc, char * argv[]) { ruby_init(); ruby_init_loadpath(); ruby_options(argc, argv); rb_define_global_function("foo", RUBY_METHOD_FUNC(test_foo), 0); ruby_run(); } pigeon% pigeon% g++ -g -O2 -I/usr/local/lib/ruby/1.6/i686-linux -I. -I/usr/local/include -o uu uu.cc -L/usr/local/lib -L/usr/local/lib/ruby/1.6/i686-linux -L/usr/local/lib -lruby -lm -lcrypt -ldl pigeon% pigeon% uu -e 'foo; GC.start; puts "exiting..."' testing... exiting... pigeon% This is 1.6.7 and I've the same result with 1.6.5 P> then my program works. It seems that the problem is rb_newobj() is No, there is no problem in rb_newobj(). What you see is that it's difficult to know which variables are on the stack and worst it's perhaps compiler dependent. Change your compiler and you can have a different result. Guy Decoux