From: "Richard P. Groenewegen" Date: 2004-10-26T05:07:55+09:00 Subject: Re: "add" function in swig and the ruby GC? > Try to post the smallest complete interface file and script that > demonstrates the problem, and then the answer may be more obvious. Thanks for the reply and the comment about auto_ptr. I'll be using that. As for the smallest complete example: //------------------------------------------------------------------- // thingy.h //------------------------------------------------------------------- #include using namespace std; class Thingy { public: Thingy() { _number = ++number; cout << "Thingy " << _number << " constructed.\n"; } ~Thingy() { cout << "Thingy " << _number << " destructed.\n"; } Thingy * add(Thingy *other) { return new Thingy(); } static int number; protected: int _number; }; //------------------------------------------------------------------- // thingy.cxx //------------------------------------------------------------------- #include "thingy.h" int Thingy::number = 0; //------------------------------------------------------------------- // thingy.i //------------------------------------------------------------------- %module thingy %{ #include "thingy.h" %} %include "thingy.h" //------------------------------------------------------------------- // irb output (slightly trimmed): //------------------------------------------------------------------- irb> require 'thingy.so' irb> a = Thingy::Thingy.new Thingy 1 constructed. irb> b = Thingy::Thingy.new Thingy 2 constructed. irb> c = a.add(b) Thingy 3 constructed. irb(main):005:0> a,b,c, = 0,0,0 irb(main):006:0> GC.start Thingy 2 destructed. Thingy 1 destructed. As you can see. Thingy 3 does not get destructed. I tried changing the function add to void add(Thingy *other, Thingy *OUTPUT) { OUTPUT = new Thingy(); } and include "typemaps.i" in the thingy.i file as (sort of) is suggested in the swig documentation but that does not work. best regards, Richard