From: Paul Brannan Date: 2002-04-19T08:00:56+09:00 Subject: GC problem in C++ extension I have a test program that looks like this: #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(); } I run it like this: ./test5 -e 'foo; GC.start; puts "exiting..."' I see output like this: exiting... testing... I would rather see output like this: testing... exiting... and, in fact, I do see that if I delete line 25. How can I make sure that the allocated Foo object gets properly destructed? Paul