Subject:
Re: [ruby-ffi] Re: c++ examples
From:
Evan Phoenix
Date:
12/7/09 12:14 PM
To:
ruby-ffi@googlegroups.com


On Dec 7, 2009, at 9:55 AM, rogerdpack wrote:

>>> In the end I think I'm going to end up having to write ruby containers
>>> for c++ objects, so I'll probably end up having to write a "real"
>>> extension after all.  Anybody know of a cross-VM compatible way to
>>> wrap C objects and call their destructors, etc.?
>>> Thanks.
>> 
>> You mean C++ objects and make them work with Ruby C API?
> 
> Good point. C++.
> 
> It appears on closer inspection that you can call something like
> MyClass.free_object
> (http://wiki.github.com/ffi/ffi/examples)
> on objects, so perhaps I could register my C++ wrapped object using
> ObjectSpace#define_finalizer and call a wrapper to the C++
> destructor.  That might work.

I do not recommend this at all. Using finalizers to release external resources is a very easy way to cause memory leaks and latent bugs.

If you need to call a destructor or cleanup function, you should explicitly call them when you know you no longer need the data.

Adding the pointer to a cleanup list that can be processed at a common place is a simple solution. 

 - Evan

> 
> -r
>