[Rd] asking for suggestions: interface for a C++ class

Seth Falcon seth at userprimary.net
Fri Sep 4 23:17:23 CEST 2009


* On 2009-09-04 at 22:54 +0200 Yurii Aulchenko wrote:
> We are at an early stage of designing an R library, which is effectively an 
> interface to a C++ library providing fast access to large matrices stored 
> on HDD as binary files. The core of the C++ library is relatively 
> sophisticated class, which we try to "mirror" using an S4 class in R. 
> Basically when a new object of that class is initiated, the C++ constructor 
> is called and essential elements of the new object are reflected as slots 
> of the R object.

Have a look at external pointers as described in the Writing R
Extensions Manual.

> Now as you can imagine the problem is that if the R object is removed using 
> say "rm" command, and not our specifically designed one, the C++ object 
> still hangs around in RAM until R session is terminated. This is not nice, 
> and also may be a problem, as the C++ object may allocate large part of 
> RAM. We can of cause replace generic "rm" and "delete" functions, but this 
> is definitely not a nice solution.

You likely want a less literal translation of your C++ object into R's
S4 system.  One slot should be an external pointer which will give you
the ability to define a finalizer to clean up when the R level object
gets gc'd.

+ seth

-- 
Seth Falcon | @sfalcon | http://userprimary.net/user



More information about the R-devel mailing list