From: Tobias Peters Date: 2004-10-26T07:54:06+09:00 Subject: Re: "add" function in swig and the ruby GC? Richard P. Groenewegen wrote: > Hi, > > I'm trying to write an add-function in C++ with swig, but I think that > ruby does not Garbage collect my objects. Example: > > class Thingy { > public: > Thingy() { } > ~Thingy() { cout << "Thingy is destructed."; } > > Thingy *add(const Thingy *other) const { > Thingy *result = new Thingy(); > // ... > return result; > } > } > > Using GC.start I can check that ruby does not destruct Thingies made > with "add". So, what is the correct way to do this? I could return a > Thingy instead of a Thingy*, but is this the preferred(TM) way? How could SWIG or Ruby possibly know that the pointer returned by your add method points to a newly created object on the heap. Swig does not interpret C++ Code, only types and signatures. The solution to your problem is in the fine SWIG manual. Hint: %newobject Tobias