Skip to content

Commit 6a51ee7

Browse files
Merge pull request #10 from aaronjg/1cf57d989190b325552b7e24c21edf034eada1e6
Add Hooks for Garbage Collection
2 parents 66b9c0f + 1cf57d9 commit 6a51ee7

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed

ext/Converters.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,9 @@ VALUE to_ruby_with_mode(SEXP robj, int mode)
272272
if (i<0) return Qnil;
273273
if (i==1) break;
274274
default:
275-
R_References = CONS(robj, R_References);
276-
SET_SYMVALUE(install("R.References"), R_References);
277-
275+
protect_robj(robj);
278276
obj = Data_Wrap_Struct(rb_const_get(rb_cObject,
279-
rb_intern("RObj")), 0, 0, robj);
277+
rb_intern("RObj")), 0, &Robj_dealloc, robj);
280278
rb_iv_set(obj,"@conversion",INT2FIX(TOP_MODE));
281279
rb_iv_set(obj,"@wrap",Qfalse);
282280
}
@@ -443,7 +441,7 @@ from_proc_table(SEXP robj, VALUE *fun)
443441
l = FIX2INT(rb_funcall(proc_table,rb_intern("size"),0));
444442

445443
obj = Data_Wrap_Struct(rb_const_get(rb_cObject,
446-
rb_intern("RObj")), 0, 0, robj);
444+
rb_intern("RObj")), 0, &Robj_dealloc, robj);
447445
rb_iv_set(obj,"@conversion",INT2FIX(TOP_MODE));
448446
rb_iv_set(obj,"@wrap",Qfalse);
449447

@@ -506,7 +504,7 @@ to_ruby_proc(SEXP robj, VALUE *obj)
506504
//Create new object based on robj and call the function
507505
//found above with it as argument
508506
tmp = Data_Wrap_Struct(rb_const_get(rb_cObject,
509-
rb_intern("RObj")), 0, 0, robj);
507+
rb_intern("RObj")), 0, &Robj_dealloc, robj);
510508
rb_iv_set(tmp,"@conversion",INT2FIX(TOP_MODE));
511509
rb_iv_set(tmp,"@wrap",Qfalse);
512510

@@ -572,7 +570,7 @@ to_ruby_class(SEXP robj, VALUE *obj)
572570
return 0; /* conversion failed */
573571

574572
tmp = Data_Wrap_Struct(rb_const_get(rb_cObject,
575-
rb_intern("RObj")), 0, 0, robj);
573+
rb_intern("RObj")), 0, &Robj_dealloc, robj);
576574
rb_iv_set(tmp,"@conversion",INT2FIX(TOP_MODE));
577575
rb_iv_set(tmp,"@wrap",Qfalse);
578576

ext/rsruby.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
/* Global list to protect R objects from garbage collection */
3737
/* This is inspired in $R_SRC/src/main/memory.c */
3838
static SEXP R_References;
39+
void protect_robj(SEXP robj){
40+
R_References = CONS(robj, R_References);
41+
SET_SYMVALUE(install("R.References"), R_References);
42+
}
3943

4044
SEXP
4145
RecursiveRelease(SEXP obj, SEXP list)
@@ -50,9 +54,9 @@ RecursiveRelease(SEXP obj, SEXP list)
5054
}
5155

5256
/* TODO: This needs implementing as a Ruby destructor for each RObj */
53-
/*static void
54-
Robj_dealloc(VALUE self)
55-
{
57+
void
58+
Robj_dealloc(VALUE self)
59+
{
5660
SEXP robj;
5761

5862
Data_Get_Struct(self, struct SEXPREC, robj);
@@ -61,13 +65,9 @@ RecursiveRelease(SEXP obj, SEXP list)
6165
SET_SYMVALUE(install("R.References"), R_References);
6266

6367
return;
64-
}*/
65-
66-
void protect_robj(){
67-
R_References = CONS(robj, R_References);
68-
SET_SYMVALUE(install("R.References"), R_References);
6968
}
7069

70+
7171
/* Obtain an R object via its name.
7272
* This is only used to get the 'get' function.
7373
* All subsequent calls go via the 'get' function itself
@@ -90,7 +90,7 @@ VALUE get_fun(VALUE self, VALUE name){
9090

9191
/* Wrap the returned R object as a ruby Object */
9292
rubyobj = Data_Wrap_Struct(rb_const_get(rb_cObject,
93-
rb_intern("RObj")), 0, 0, robj);
93+
rb_intern("RObj")), 0, &Robj_dealloc , robj);
9494
rb_iv_set(rubyobj,"@conversion",INT2FIX(conversion));
9595
rb_iv_set(rubyobj,"@wrap",Qfalse);
9696

ext/rsruby.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,6 @@ VALUE RObj_init_lcall(VALUE self, VALUE args);
8484
VALUE RObj_to_ruby(VALUE self, VALUE args);
8585
int make_argl(VALUE args, SEXP *e);
8686
void protect_robj(SEXP robj);
87+
void Robj_dealloc(VALUE self);
8788
#endif
8889

0 commit comments

Comments
 (0)