Skip to content
Prev 33907 / 63424 Next

asking for suggestions: interface for a C++ class

Yurii,
On Sep 4, 2009, at 16:54 , Yurii Aulchenko wrote:

            
[FWIW there are already several packages that do waht you describe -  
see e.g. ff, bigMemory, nws, ...]
You must have some link between the S4 object and your C++ object -  
ideally an external pointer - so all you have to do is to attach a  
finalizer to it via R_RegisterCFinalizer or R_RegisterCFinalizerEx. In  
that finalizer you simply free the C++ object and all is well.

Note that R uses a garbage collector so the object won't go away  
immediately after it went out of scope - only after R thinks it needs  
to reclaim memory. You can use gc() to force garbage collection to  
test it.
... and it doesn't tackle the issue - objects can go out of scope by  
other means than just rm(), e.g.:
f <- function() { ...; myGreatObject }
f()
# the great object is gone now since it was not assigned anywhere
Cheers,
Simon