From seth at userprimary.net  Fri Sep  4 23:17:23 2009
From: seth at userprimary.net (Seth Falcon)
Date: Fri, 4 Sep 2009 14:17:23 -0700
Subject: [Rd] asking for suggestions: interface for a C++ class
In-Reply-To: <91FB958D-CFB2-45CD-8EF1-2206E04C4EE6@erasmusmc.nl>
References: <91FB958D-CFB2-45CD-8EF1-2206E04C4EE6@erasmusmc.nl>
Message-ID: <20090904211723.GP12397@ziti.local>

* 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



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