[Rcpp-devel] c++ class as argument in rcpp function (using modules)

Yasir Suhail yusuhail at gmail.com
Thu Dec 22 00:29:26 CET 2011


Thanks Darren, Douglas, Richard, and Dirk!

I can compile with the following definitions:

World(SEXP x) {
Rcpp::XPtr<World> ptr(x);
msg = ptr->msg;
}

and

World(SEXPREC* &w2) {
msg = ((World*)w2)->msg;
}

With the SEXPREC* & definition, I get a runtime segfault:
> a <- new(World,"sdf")
> b <- new(World,"bbb")
> a$greet( )
[1] "sdf"
> b$greet( )
[1] "bbb"
> a$addMsg( b)

 *** caught segfault ***
address 0x0, cause 'unknown'

Traceback:
 1: .External(list(name = "CppMethod__invoke_void", address = <pointer:
0x7f838b4034b0>,     dll = list(name = "Rcpp", path =
"/opt/local/lib/R/library/Rcpp/libs/x86_64/Rcpp.so",         dynamicLookup
= TRUE, handle = <pointer: 0x7f838b60f330>,         info = <pointer:
0x10ee1ec18>), numParameters = -1L),     <pointer: 0x7f838b4e6f30>,
<pointer: 0x7f838b4f7350>, .pointer,     ...)
 2: a$addMsg(b)


With the SEXP and Rcpp::XPtr definition, it also caused segfault:
 *** caught segfault ***
address 0xfffffa, cause 'memory not mapped'

Traceback:
 1: .External(list(name = "CppMethod__invoke_void", address = <pointer:
0x7fccb0c370a0>,     dll = list(name = "Rcpp", path =
"/opt/local/lib/R/library/Rcpp/libs/x86_64/Rcpp.so",         dynamicLookup
= TRUE, handle = <pointer: 0x7fccb0e09680>,         info = <pointer:
0x10dcc3bc0>), numParameters = -1L),     <pointer: 0x7fccb0cee700>,
<pointer: 0x7fccb0ca5880>, .pointer,     ...)
 2: b$addMsg(a)

Perhaps there is some problem with the pointer conservation?


On Wed, Dec 21, 2011 at 5:56 PM, Darren Cook <darren at dcook.org> wrote:

> >     void addMsg(World& w2) {
> > msg = msg + w2.msg;
> >     }
>
> BTW, this could be (and should be)
>  void addMsg(const World& w2) { ...}
>
> > error: no matching function for call to ‘World::World(SEXPREC*&)’
>
> How about something like:
>
>  World(SEXPREC* &w2){
>    msg= ((World*)w2)->msg;
>  }
>
> Darren
>
> --
> Darren Cook, Software Researcher/Developer
>
> http://dcook.org/work/ (About me and my work)
> http://dcook.org/blogs.html (My blogs and articles)
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20111221/badb2c9b/attachment.htm>


More information about the Rcpp-devel mailing list