Hi,<div><br></div><div>Dirk, thanks for uploading the next version, because it does sort of help here. The error now with Rcpp 0.9.8 is "expecting an external pointer" from XPtr on the following very toy example. It has something to do with the type of SEXP in the constructor, but I am not very familiar with the Rcpp internals to be able to figure it out.<div>
<br></div><div><div><font color="#006600" face="'arial narrow', sans-serif">#include <Rcpp.h></font></div><div><font color="#006600" face="'arial narrow', sans-serif">class World {</font></div><div><font color="#006600" face="'arial narrow', sans-serif">public:</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"> World() : msg("hello"){}</font></div><div><font color="#006600" face="'arial narrow', sans-serif"> World(const std::string s) : msg(s){}</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"> void set(std::string msg) { this->msg = msg; }</font></div><div><font color="#006600" face="'arial narrow', sans-serif"> std::string greet() const { return msg; }</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"> void addMsg(const World w2) {</font></div><div><font color="#006600" face="'arial narrow', sans-serif"> this->msg = this->msg + w2.greet();</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"> }</font></div><div><font color="#006600" face="'arial narrow', sans-serif"> World(SEXP x) {</font></div><div><font color="#006600" face="'arial narrow', sans-serif"> Rcpp::XPtr<World> ptr(x);</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"> msg = ptr->greet();</font></div><div><font color="#006600" face="'arial narrow', sans-serif"> }</font></div><div><font color="#006600" face="'arial narrow', sans-serif">private:</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"> std::string msg;</font></div><div><font color="#006600" face="'arial narrow', sans-serif">};</font></div><div><div><font color="#006600" face="'arial narrow', sans-serif">RCPP_MODULE(yada){</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"> using namespace Rcpp ;</font></div><div><font color="#006600" face="'arial narrow', sans-serif"> class_<World>( "World" )</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"> .constructor()</font></div><div><font color="#006600" face="'arial narrow', sans-serif"> .constructor<std::string>()</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"> .method( "greet", &World::greet , "get the message" )</font></div><div><font color="#006600" face="'arial narrow', sans-serif"> .method( "set", &World::set , "set the message" )</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"> .method( "addMsg", &World::addMsg , "add the message from another object" );</font></div><div><font color="#006600" face="'arial narrow', sans-serif">}</font></div>
</div><div><br></div><div>And then in R:</div><div><div><br></div><div><font color="#cc0000" face="'arial narrow', sans-serif">>library(yada)</font></div><div><font color="#cc0000" face="'arial narrow', sans-serif">>a <- new(World,"this is a")</font></div>
<div><font color="#cc0000" face="'arial narrow', sans-serif">>b <- new(World,"this is b")</font></div><div><font color="#cc0000" face="'arial narrow', sans-serif">>a$addMsg(b)</font></div>
<div><font color="#cc0000" face="'arial narrow', sans-serif">Error in a$addMsg(b) : expecting an external pointer</font></div></div><div><br></div>I'd appreciate if someone could shed some light on it.</div><div>
<br></div><div>Thanks!</div><div><br><div class="gmail_quote">On Wed, Dec 21, 2011 at 7:52 PM, Yasir Suhail <span dir="ltr"><<a href="mailto:yusuhail@gmail.com">yusuhail@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi,<div><br></div><div>Other than the approaches mentioned, I can also specialize an as<World>(SEXP) as<div><br></div><div><div> template <></div><div> World as<World>(SEXP x) {</div><div><span style="white-space:pre-wrap">        </span>Rcpp::XPtr<World> ptr(x);</div>
<div><span style="white-space:pre-wrap">        </span>return World(ptr->greet());</div><div> }</div><div><br></div><div>However, this also gives segfault at runtime when addMsg is called. The final code, with alternative implementations commented out for experimentation is:</div>
<div><br></div><div><div><font color="#006600" face="'arial narrow', sans-serif">#include <RcppCommon.h></font></div><div><font color="#006600" face="'arial narrow', sans-serif">#include <string></font></div>
<div class="im">
<div><font color="#006600" face="'arial narrow', sans-serif">class World {</font></div><div><font color="#006600" face="'arial narrow', sans-serif">public:</font></div><div><font color="#006600" face="'arial narrow', sans-serif"> World() : msg("hello"){}</font></div>
</div><div><font color="#006600" face="'arial narrow', sans-serif"> World(const std::string s) : msg(s){}</font></div><div class="im"><div><font color="#006600" face="'arial narrow', sans-serif"> void set(std::string msg) { this->msg = msg; }</font></div>
</div><div><font color="#006600" face="'arial narrow', sans-serif"> std::string greet() const { return msg; }</font></div><div><font color="#006600" face="'arial narrow', sans-serif"> void addMsg(const World w2) {</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"><span style="white-space:pre-wrap">        </span>this->msg = this->msg + w2.greet();</font></div><div><font color="#006600" face="'arial narrow', sans-serif"> }</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif">// World(SEXP x);</font></div><div><font color="#006600" face="'arial narrow', sans-serif">/* World(SEXPREC* &w2) {</font></div><div>
<font color="#006600" face="'arial narrow', sans-serif"><span style="white-space:pre-wrap">        </span>msg = ((World*)w2)->greet();</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"> }</font></div><div><font color="#006600" face="'arial narrow', sans-serif">*/</font></div><div><font color="#006600" face="'arial narrow', sans-serif">private: </font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"> std::string msg;</font></div><div><font color="#006600" face="'arial narrow', sans-serif">};</font></div><div><font color="#006600" face="'arial narrow', sans-serif">namespace Rcpp{</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"><br></font></div><div><font color="#006600" face="'arial narrow', sans-serif">template <> SEXP wrap(const World& object);</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif">template <> World as<World>(SEXP x);</font></div><div><font color="#006600" face="'arial narrow', sans-serif">}</font></div><div><font color="#006600" face="'arial narrow', sans-serif">#include <Rcpp.h></font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"><br></font></div><div><font color="#006600" face="'arial narrow', sans-serif"><br></font></div><div><font color="#006600" face="'arial narrow', sans-serif">namespace Rcpp {</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"> template <></font></div><div><font color="#006600" face="'arial narrow', sans-serif"> SEXP wrap( const World& object ){</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"> Language call( "new", Symbol( "World" ), object.greet() ) ;</font></div><div><font color="#006600" face="'arial narrow', sans-serif"> return call.eval() ;</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"> }</font></div><div><font color="#006600" face="'arial narrow', sans-serif"> template <></font></div><div><font color="#006600" face="'arial narrow', sans-serif"> World as<World>(SEXP x) {</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"><span style="white-space:pre-wrap">        </span>Rcpp::XPtr<World> ptr(x);</font></div><div><font color="#006600" face="'arial narrow', sans-serif"><span style="white-space:pre-wrap">        </span>return World(ptr->greet());</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"> }</font></div><div><font color="#006600" face="'arial narrow', sans-serif">}</font></div><div><font color="#006600" face="'arial narrow', sans-serif"><br>
</font></div><div><font color="#006600" face="'arial narrow', sans-serif"><br></font></div><div><font color="#006600" face="'arial narrow', sans-serif">/*World::World(SEXP x) {</font></div><div><font color="#006600" face="'arial narrow', sans-serif"><span style="white-space:pre-wrap">        </span>Rcpp::XPtr<World> ptr(x);</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"><span style="white-space:pre-wrap">        </span>msg = ptr->greet();</font></div><div><font color="#006600" face="'arial narrow', sans-serif">}*/</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"><br></font></div><div><div><font color="#006600" face="'arial narrow', sans-serif">//...</font></div></div></div><div><div><font color="#006600" face="'arial narrow', sans-serif"><br>
</font></div><div><font color="#006600" face="'arial narrow', sans-serif">RCPP_MODULE(yada){</font></div><div><font color="#006600" face="'arial narrow', sans-serif"><span style="white-space:pre-wrap">        </span>using namespace Rcpp ;</font></div>
</div><div><div><font color="#006600" face="'arial narrow', sans-serif"><span style="white-space:pre-wrap">        </span>class_<World>( "World" )</font></div><div><font color="#006600" face="'arial narrow', sans-serif"><span style="white-space:pre-wrap">        </span> // expose the default constructor</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"><span style="white-space:pre-wrap">        </span> .constructor()</font></div><div><font color="#006600" face="'arial narrow', sans-serif"> .constructor<std::string>()</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"><span style="white-space:pre-wrap">        </span> <span style="white-space:pre-wrap">        </span>.method( "greet", &World::greet , "get the message" )</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"><span style="white-space:pre-wrap">                </span>.method( "set", &World::set , "set the message" )</font></div>
<div><font color="#006600" face="'arial narrow', sans-serif"><span style="white-space:pre-wrap">                </span>.method( "addMsg", &World::addMsg , "add the message from another object" );</font></div>
</div><div><font color="#006600" face="'arial narrow', sans-serif">}</font></div><div><br></div><div>Then, running with R -d gdb</div><div><div>> a <- new(World,"hello ")</div><div>> b <- new(World,"worlds")</div>
<div>> a$greet( )</div><div>[1] "hello "</div><div>> a$addMsg( b)</div><div><br></div><div>Program received signal EXC_BAD_ACCESS, Could not access memory.</div><div>Reason: KERN_INVALID_ADDRESS at address: 0x0000000011000078</div>
<div>0x00000001042032df in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string ()</div><div>(gdb) bt</div><div>#0 0x00000001042032df in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string ()</div>
<div>#1 0x0000000104358b18 in Rcpp::as<World> ()</div><div>#2 0x000000010435d564 in Rcpp::CppMethod1<World, void, World>::operator() ()</div><div>#3 0x000000010435eadb in Rcpp::class_<World>::invoke_void ()</div>
<div>#4 0x0000000104164670 in CppMethod__invoke_void ()</div><div>#5 0x00000001000928e9 in do_External ()</div><div>#6 0x00000001000c4af7 in Rf_eval ()</div><div>#7 0x00000001000c6d99 in do_begin ()</div><div>#8 0x00000001000c48af in Rf_eval ()</div>
<div>#9 0x00000001000c7cf3 in Rf_applyClosure ()</div><div>#10 0x00000001000c4778 in Rf_eval ()</div><div>#11 0x00000001000fed75 in Rf_ReplIteration ()</div><div>#12 0x00000001000ff021 in R_ReplConsole ()</div><div>#13 0x00000001000ff590 in run_Rmainloop ()</div>
<div>#14 0x0000000100000e8b in main ()</div><div>(gdb) </div></div><div><br></div><div>Thanks!</div></div><div><div class="h5"><br><div class="gmail_quote">On Wed, Dec 21, 2011 at 7:41 PM, Darren Cook <span dir="ltr"><<a href="mailto:darren@dcook.org" target="_blank">darren@dcook.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello Yasir,<br>
Could you post your complete (minimal) code to the list. Then someone<br>
might be curious enough to try reproducing the problem.<br>
<span><font color="#888888"><br>
Darren<br>
</font></span><div><br>
<br>
On 2011-12-22 08:29, Yasir Suhail wrote:<br>
> Thanks Darren, Douglas, Richard, and Dirk!<br>
><br>
> I can compile with the following definitions:<br>
><br>
> World(SEXP x) {<br>
> Rcpp::XPtr<World> ptr(x);<br>
> msg = ptr->msg;<br>
> }<br>
<br>
</div>> ...<br>
<div><div><br>
<br>
--<br>
Darren Cook, Software Researcher/Developer<br>
<br>
<a href="http://dcook.org/work/" target="_blank">http://dcook.org/work/</a> (About me and my work)<br>
<a href="http://dcook.org/blogs.html" target="_blank">http://dcook.org/blogs.html</a> (My blogs and articles)<br>
</div></div></blockquote></div><br></div></div></div>
</blockquote></div><br></div></div>