[Rcpp-devel] Registering a custom delete_finalizer for my XPtrs

Steve Lianoglou mailinglist.honeypot at gmail.com
Mon Jul 4 01:02:59 CEST 2011


Preamble: My C++ is quite ... hmmm, does anybody know any good
euphemisms for "weak"?. So, sorry if this (or questions that will
likely follow) is too basic.

I've been using Rcpp to help make a more R-like wrapper library to the
shogun-toolbox[1]. So far, it's been pretty fun, and (to my surprise
:-) I've been able to get basic things working, such as building a
handful of different types of SVMs. A testament to the quality of both
Rcpp and the shogun-toolbox code, since, as I said, my C++ isn't "the

I know I'm not making the most out of using Rcpp, and wanted to write
my code in a more Rcpp-inspired manner before I get too deep. (I
actually don't know if Rcpp-modules would work here, but maybe will
try that in the future as well).

So, in my C++ code, I wire different objects together, and return a
pointer to the shogun object that I just made. Currently I've got a
simple function that wraps pointers to shogun objects into externalptr
SEXP's and registers my custom finalizer, which basically does what
you'd expect, eg:

SEXP SG2SEXP(shogun::CSGObject *o) {
    SEXP xp = R_MakeExternalPtr(o, R_NilValue, R_NilValue);
    R_RegisterCFinalizer(xp, _shogun_ref_count_down);
    return xp;

What I thought a more Rcpp-inspired thing to do is to instead
instantiate pointers to shogun objects using
Rcpp::XPtr<SomeShogunObject>and just rely on the auto-wrapping to make
things "more clean", maybe like:

// ...
Rcpp::XPtr<SomeShogunObject> so(new SomeShogunObject(what,ever), true);
// ...
return so;

But I don't want Rcpp's "vanilla" delete_finalizer to be invoked on my
object's R-side destruction -- I want whatever I've defined in my
`_shogun_ref_count_down` function to be used instead.

(this is where my n00b-ness becomes self-evident):

Since XPtr::setDeleteFinalizer doesn't take a parameter for the
finalizer function to use, I thought I the "expected" way to achieve
this is through template specialization of delete_finalizer in my
package. Since every object in the shogun-toolbox extends CSGObject, I
thought this would be easy enough and maybe do something like:

void delete_finalizer<shogun::CSGObject>(SEXP p) { /* something special */ }

But that doesn't work/compile -- I guess because this specialization
and Rcpp's delete_finalizer definition aren't in the same compilation
units, as described here:


-- or maybe I have to stick it in the Rcpp namespace somehow, like the
custom wrap/as trick?

So -- long story short ... is there a more Rcpp/spiffy way for me to
register my own delete_finalizer function, or should I just keep going
with my SG2SEXP function for now, and save the Rcpp-swagger for when I
see if Rccp-modules is a good fit (and just use its *.finalizer()



[1] shogun-toolbox: http://www.shogun-toolbox.org/

Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact

More information about the Rcpp-devel mailing list