<div dir="ltr">Thank you for the feedback Dirk. Fair enough.<div><br></div><div>I think the issue I have is similar to this (I want to set StoragePolicy = false for the XPtr), so I sort of have a work around going: <font color="#1155cc"><u><a href="http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2013-February/005273.html">http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2013-February/005273.html</a></u></font></div><div><br></div><div>I would like 'res' to be a 'non-owning' reference pointer in R to A in B. I don't know how this could be done directly (or if it's possible) with a .finalizer ... do you mind elaborating a little? (The Rcpp module vignette is very brief on .finalizer). Or are there any examples anywhere that demonstrate what you mean?</div><div><br></div><div>Thanks again.</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Nov 10, 2014 at 1:45 PM, Dirk Eddelbuettel <span dir="ltr"><<a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5"><br>
On 10 November 2014 at 13:16, Claymore Marshall wrote:<br>
| I want to expose to R several C++ classes from a library with Rcpp modules. <br>
| I'm trying to imitate a similar set of Python modules that were made for the<br>
| same library.<br>
|<br>
| In Boost.Python, with .def it is possible to specify the return_value_policy. <br>
| Is it also possible to do this in a straight forward way with Rcpp modules when<br>
| exposing C++ class methods?<br>
|<br>
| The following simplified example illustrates a problem I face with using Rcpp<br>
| modules. When R deletes 'res', R crashes shortly after (a segfault?). <br>
|<br>
| With a Python module, this kind of problem apparently doesn't come about by<br>
| setting in .def the following: return_value_policy<reference_existing_object>()<br>
| <br>
|<br>
| Is there an easy way to get around this problem with Rcpp modules with minimal<br>
| code?<br>
|<br>
| #include <Rcpp.h><br>
| using namespace Rcpp;<br>
|<br>
| class A {<br>
|<br>
| public:<br>
| <br>
| A(int x_) : x(x_){}<br>
|<br>
| int x;<br>
| };<br>
|<br>
| class B {<br>
| <br>
| public:<br>
| B():m_A(10) {}<br>
| <br>
| A get_an_A(){<br>
| A an_A(3);<br>
| return an_A;<br>
| }<br>
| A * get_member_A() { return & m_A; }<br>
| <br>
| private:<br>
| A m_A;<br>
| };<br>
|<br>
| RCPP_EXPOSED_CLASS(A)<br>
| RCPP_EXPOSED_CLASS(B)<br>
|<br>
| RCPP_MODULE(AB_mod){<br>
| using namespace Rcpp ;<br>
| class_<A>("A")<br>
| .constructor<int>()<br>
| .field("x", &A::x)<br>
| ;<br>
| class_<B>("B")<br>
| .constructor()<br>
| .method("get_an_A",&B::get_an_A)<br>
| .method("get_member_A", &B::get_member_A)<br>
| ;<br>
| }<br>
|<br>
|<br>
|<br>
| ------------------------<br>
|<br>
| R code:<br>
|<br>
| A_eg <- new(A, 10)<br>
| B_eg <- new(B) <br>
|<br>
| res <- B_eg$get_member_A()<br>
|<br>
| rm(res)<br>
| # R crashes shortly after running further commands ...<br>
| # How can this error be prevented?<br>
<br>
</div></div>By not removing res?<br>
<br>
By adding a finalizer / destructor for it?<br>
<br>
I don't use modules for things I add / remove frequently from the heap. I<br>
generally just create an object "and keep it". That usage works for me.<br>
<span class="HOEnZb"><font color="#888888"><br>
Dirk<br>
<br>
--<br>
<a href="http://dirk.eddelbuettel.com" target="_blank">http://dirk.eddelbuettel.com</a> | @eddelbuettel | <a href="mailto:edd@debian.org">edd@debian.org</a><br>
</font></span></blockquote></div><br></div>