Paul Brannan
10/25/2004 3:28:00 PM
On Mon, Oct 25, 2004 at 06:51:21AM +0900, Richard P. Groenewegen wrote:
> class Thingy {
> public:
> Thingy() { }
> ~Thingy() { cout << "Thingy is destructed."; }
>
> Thingy *add(const Thingy *other) const {
> Thingy *result = new Thingy();
> // ...
> return result;
> }
> }
First of all, Ruby will never collect result in the case that an
exception is thrown. Better would be to write:
Thingy *add(const Thingy *other) const {
std::auto_ptr<Thingy> result(new Thingy);
// ...
return result.release();
}
Secondly, because of Ruby's conservative GC, there is no guarantee that
the objects will ever be destroyed as long as your program is running.
If you need to be able to guarantee that a particular resource (e.g. a
socket or a lock) needs to be freed, you will be better off not relying
on the garbage collector but instead using a different mechanism, such
as blocks or an explicit method call to free the resource.
Now that we have that out of the way, you're probably still wondering
why your objects don't get cleaned up. Well, swig should clean up your
objects, but depending on how you've written your swig interface file
and/or your ruby script, that might not happen. Try to post the
smallest complete interface file and script that demonstrates the
problem, and then the answer may be more obvious.
Paul