From: "Asbjørn Reglund Thorsen" Date: 2004-10-26T16:24:17+09:00 Subject: Re: "add" function in swig and the ruby GC? There is some interessting reading about this quind of problem in the SWIG manual page 398 chapter 27.8.5. On Tue, 26 Oct 2004, Tobias Peters wrote: > 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 > >