logo       

Re: how to prevent objects from being garbage collected.: msg#00132

programming.swig

Subject: Re: how to prevent objects from being garbage collected.

meinrad recheis wrote:
is there a way to prevent objects from being destructed by the ruby garbage collector or at the end of the script, without having to write markfunks for the owner of these objects?

I haven't yet had a chance to study the responses so far, but I wanted to let you know that I hit a similar problem, and have implemented a slightly ugly but effective solution.

In my case, the C++ objects might be considered to have "shared" ownership, where they could legitimately be deleted from the ruby side, or by the C++ library that I am wrapping. The C++ side will not ask my permission before deleting the objects, so it is important that I be able to detect later that it has happened.

Unfortunately, if C++ deletes the object first, then Ruby invokes the free_xxx method that was passed at the time the object was created, and that method has no protection against re-deleting the already-deleted object.

Here's my approach so far. Note that I have only ONE c++ class per source code file, which simplifies the map handling:

1. Make sure every class that might have this problem is wrapped with a director.
2. Declare and define a static hash/map variable. Each entry in the map will have the C++ object as the key, and the associated Ruby object as the value.
3. In the director constructor, add this/self to the map.
4. In the director destructor, remove this/self from the map.
5. In the free_xxx function, only delete the pointer if it is in the map.

This seems to have solved the problem for me.

I am already having to do post-processing of each swigged file for several other reasons, so this is just another pesky step for me. Eventually, I would like to see some feature like this built into SWIG/Ruby (and other language bindings, if it is also a problem there).

Perhaps one of the other replies in this thread has suggested a better approach. That would be great.

Kevin
_______________________________________________
Swig maillist - Swig@xxxxxxxxxxxxxxx
http://mailman.cs.uchicago.edu/mailman/listinfo/swig



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise