On Sat, Aug 24, 2002 at 04:19:06AM +0900, William Djaja Tjokroaminata wrote:
> Thanks for all the responses.  So I guess we are already dealing with a
> pretty reasonable gc and I just have to make effort to get around it.
> 
> I have just one thing that I would like to get clarified.  Suppose the
> code is as simple as
> 
>     loop (in C):
>         create a (simple) Ruby object
>         interact with the user through this Ruby object
>         when the user method returns, delete this Ruby object
> 
> If I try to modify the sweep part by deleting this Ruby object manually,
> will it likely to break?

Another way to do this would be:

  loop do
    Foo.allocate do |foo|
      interact_with_user(foo)
    end
  end

Foo.allocate creates the foo object and yields it to the block.  When
the block exits, it breaks-down but does not destroy the object.  For
example, if you pass a block to File.open, then the file gets closed
when the block exits.  If you try to pass the file object out of the
block and then use it, then you will get an exception, because you are
trying to read or write to a closed file.

In your case, you may want to simply deallocate memory when the block
exits, and then mark the object as dead.  You can use Object#extend as a
cheap way to disable methods on the object that might be dangerous if
called on a half-dead object.

This has the added benefit of being exception-safe.

Paul