[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

C Extension Cleanup

Justin Bonnar

10/15/2006 8:02:00 PM

I'm writting a wrapper for a C library[1] and need to perform some
clean-up when the Ruby process exists. In my Init function, I create a
C global variable that needs to be explicitly freed after the garbage
collector has finished cleaning up all Ruby objects. How should this be
done?

-Justin

--
Posted via http://www.ruby-....

15 Answers

???? ??????

10/15/2006 8:27:00 PM

0


On Oct 16, 2006, at 12:01 AM, Justin Bonnar wrote:

> I'm writting a wrapper for a C library[1] and need to perform some
> clean-up when the Ruby process exists. In my Init function, I
> create a
> C global variable that needs to be explicitly freed after the garbage
> collector has finished cleaning up all Ruby objects. How should
> this be
> done?
>

As far as I understand, there is not such handler. However, You may
register
global object, that will be destroyed when ruby exits. This global
object
will start your cleaner.

Justin Bonnar

10/15/2006 9:23:00 PM

0

Max Lapshin wrote:
> As far as I understand, there is not such handler. However, You may
> register
> global object, that will be destroyed when ruby exits. This global
> object
> will start your cleaner.

The problem is, it's not a Ruby object and I need to run some an outside
function to facilitate the clean-up. I tried creating and wrapping the
object in an anonymous class, registering the class and the instance as
global but I had segmentation fault issues (it was being freed early for
some reason.)

--
Posted via http://www.ruby-....

Ryan Davis

10/15/2006 9:58:00 PM

0


On Oct 15, 2006, at 1:01 PM, Justin Bonnar wrote:

> I'm writting a wrapper for a C library[1] and need to perform some
> clean-up when the Ruby process exists. In my Init function, I
> create a
> C global variable that needs to be explicitly freed after the garbage
> collector has finished cleaning up all Ruby objects. How should
> this be
> done?

Chapter 21 of the Pickaxe (2nd ed) is what you want. It covers this
topic fairly well.


Justin Bonnar

10/15/2006 10:17:00 PM

0

Ryan Davis wrote:
> Chapter 21 of the Pickaxe (2nd ed) is what you want. It covers this
> topic fairly well.

I'm not exposing a Ruby object though. Also, the order in which the
objects are freed when the interpreter process exits matters: if my
world object is freed before the instances, there will be a segmentation
fault. The order in which circularly referenced objects are freed at
exit seems to be undefined, so maintaining a reference for every object
won't work well.

As I said before, I tried something along the lines of:

my_global_object *world;
VALUE world_holder;

void Init_extension()
{
world = create_world();
world_holder = Data_Wrap_Struct(rb_cObject, 0,
free_my_global_object, world);
rb_global_variable(world_holder);
}

but still get segmentation faults.

--
Posted via http://www.ruby-....

Joel VanderWerf

10/15/2006 10:38:00 PM

0

Justin Bonnar wrote:
> Ryan Davis wrote:
>> Chapter 21 of the Pickaxe (2nd ed) is what you want. It covers this
>> topic fairly well.
>
> I'm not exposing a Ruby object though. Also, the order in which the
> objects are freed when the interpreter process exits matters: if my
> world object is freed before the instances, there will be a segmentation
> fault. The order in which circularly referenced objects are freed at
> exit seems to be undefined, so maintaining a reference for every object
> won't work well.
>
> As I said before, I tried something along the lines of:
>
> my_global_object *world;
> VALUE world_holder;
>
> void Init_extension()
> {
> world = create_world();
> world_holder = Data_Wrap_Struct(rb_cObject, 0,
> free_my_global_object, world);
> rb_global_variable(world_holder);
> }
>
> but still get segmentation faults.

Your free_my_global_object() function should be called at exit. Is it?

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Justin Bonnar

10/15/2006 10:48:00 PM

0

Joel VanderWerf wrote:
> Your free_my_global_object() function should be called at exit. Is it?

It is. The problem is, it's called before freeing other wrapped
datastructures whose free funtions depend on the existance of the world
object. This results in as segmentation fault or a bus error.

I need to ensure that my free_my_global_object function isn't called
until all of my other objects have been freed.

--
Posted via http://www.ruby-....

Ara.T.Howard

10/15/2006 11:50:00 PM

0

Nobuyoshi Nakada

10/16/2006 1:59:00 AM

0

Hi,

At Mon, 16 Oct 2006 07:17:12 +0900,
Justin Bonnar wrote in [ruby-talk:219893]:
> rb_global_variable(world_holder);
rb_global_variable(&world_holder);

> but still get segmentation faults.

Naturally.

--
Nobu Nakada

Justin Bonnar

10/16/2006 2:08:00 AM

0

Nobuyoshi Nakada wrote:
> rb_global_variable(&world_holder);

It still faults with a Bus Error, except this time it'll wait until
finish running my test suite instead of faulting at unrelated points
half-way through.

--
Posted via http://www.ruby-....

Tartarus

10/22/2008 9:20:00 PM

0

On Oct 22, 3:15 pm, TigerLuck <TigerLuck@wild_kingdom.afr> wrote:
> Tom Sr. wrote:
> > On Oct 22, 3:15 pm, R...@ix.com wrote:
> >> The Mainstream Drive-By Media always lies.
> >> McCain will win in a landslide.
>
> > You apparently have failed to notice that politically the Fox Channel
> > is only slightly left of Hitler.
>
> >  -Tom Sr.
>
> That makes no sense in any context whatsoever.

Makes sense to me!

Tartarus!