[Rcpp-devel] Create and access several instances of a C++ class from R

soeren.vogel at uzh.ch soeren.vogel at uzh.ch
Wed May 18 09:52:10 CEST 2011

Hello Dirk

That was exactly the trick! Thanks a lot! Our class and code work so far, but it may take more time to debug or find bad design. Perhaps we will be back with more detailed questions. Anyway, one question already emerged. When librarying the class, assigning an object, do some calculations, whatever -- everything goes fine. Yet, when using R CMD BATCH on a file listing the same commands as by hand, R produces some nice information at the end, however, here it produces an segmentation fault, which does -- according to my small knowledge -- say something bad:

> proc.time()
   user  system elapsed 
  0.702   0.017   0.714 
R(72992) malloc: *** error for object 0x2c9b604: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug

 *** caught segfault ***
address 0x4909d58, cause 'memory not mapped'

 1: save(list = ls(envir = .GlobalEnv, all.names = TRUE), file = outfile,     version = version, ascii = ascii, compress = compress, envir = .GlobalEnv,     precheck = FALSE)
 2: save.image(name)
 3: sys.save.image(".RData")
aborting ...

Any idea what goes wrong there?

Sören + Carlo

On 14.05.2011, at 18:17, Dirk Eddelbuettel wrote:


On 13 May 2011 at 20:07, soeren.vogel at uzh.ch wrote:
| Compilation with R CMD CHECK FOO fails with the following error in 00install.out:
| Error in dyn.load(file, DLLpath = DLLpath, ...) : 
|   unable to load shared object '/Users/sovo/GUTS/FOO.Rcheck/FOO/libs/i386/FOO.so':
|   dlopen(/Users/sovo/GUTS/FOO.Rcheck/FOO/libs/i386/FOO.so, 6): Symbol not found: __ZN3FOOC1Ev
|   Referenced from: /Users/sovo/GUTS/FOO.Rcheck/FOO/libs/i386/FOO.so
|   Expected in: flat namespace
| The (original) class and its functions compile fine with R CMD SHLIB. So we guess that this error has something to do with the Rcpp modules implementation.

It took me a few minutes with trial and error, but essentially the mistake
seems to have been this:

| > | 		FOO();
| > | 		~FOO();

You declared the constructor and deconstructor but there was no code
anywhere.  A pair of empty braces ' {}; '  is all it takes.  

In the process, I renamed things from 'foo' (just to be plain) to 'Baz'.  So
the file is now

// baz_module.cpp 
#include <Rcpp.h>

// from Baz.h
class Baz
 double dtau;
 std::vector<double> D, ee, ff, S;

 int M;
 std::vector<double> C, t, s, par;
 std::vector<int> y;

 Baz() {};
 ~Baz() {};

 double do_bar(std::vector<double> z);

// from Baz.cpp
double Baz::do_bar(std::vector<double> z) {
 // whatever it does
 return 42.0;			// need to return something here

 using namespace Rcpp ;
 class_<Baz>( "Baz" )
   .field( "M" , &Baz::M )
   .field( "C" , &Baz::C )
   .method( "do_bar", &Baz::do_bar )

and with the line 

   RcppModules: yada, baz

we get this module loaded:

R> library(mypackage)
R> Baz
C++ class 'Baz' <0x1c37db0>

   std::vector<double, std::allocator<double> > C
   int M

    double do_bar(std::vector<double, std::allocator<double> >)  

R> bb <- new(Baz)
R> bb
C++ object <0x18c8f90> of class 'Baz' <0x1c37db0>
R> bb$M <- 12
R> print(bb$M)
[1] 12
R> print(bb$C)
R> bb$C <- seq(1.1, 5.5, by=1.1)
R> bb$C
[1] 1.1 2.2 3.3 4.4 5.5
R> R> print(bb$do_bar(1:4))
[1] 42

I hope you can take it from here. 

Cheers, Dirk

Gauss once played himself in a zero-sum game and won $50.
                     -- #11 at http://www.gaussfacts.com

More information about the Rcpp-devel mailing list