On 10 November 2014 at 13:16, Claymore Marshall wrote: | I want to expose to R several C++ classes from a library with Rcpp modules. | I'm trying to imitate a similar set of Python modules that were made for the | same library. | | In Boost.Python, with .def it is possible to specify the return_value_policy. | Is it also possible to do this in a straight forward way with Rcpp modules when | exposing C++ class methods? | | The following simplified example illustrates a problem I face with using Rcpp | modules. When R deletes 'res', R crashes shortly after (a segfault?). | | With a Python module, this kind of problem apparently doesn't come about by | setting in .def the following: return_value_policy<reference_existing_object>() | | | Is there an easy way to get around this problem with Rcpp modules with minimal | code? | | #include <Rcpp.h> | using namespace Rcpp; | | class A { | | public: | | A(int x_) : x(x_){} | | int x; | }; | | class B { | | public: | B():m_A(10) {} | | A get_an_A(){ | A an_A(3); | return an_A; | } | A * get_member_A() { return & m_A; } | | private: | A m_A; | }; | | RCPP_EXPOSED_CLASS(A) | RCPP_EXPOSED_CLASS(B) | | RCPP_MODULE(AB_mod){ | using namespace Rcpp ; | class_<A>("A") | .constructor<int>() | .field("x", &A::x) | ; | class_<B>("B") | .constructor() | .method("get_an_A",&B::get_an_A) | .method("get_member_A", &B::get_member_A) | ; | } | | | | ------------------------ | | R code: | | A_eg <- new(A, 10) | B_eg <- new(B) | | res <- B_eg$get_member_A() | | rm(res) | # R crashes shortly after running further commands ... | # How can this error be prevented?
By not removing res? By adding a finalizer / destructor for it? I don't use modules for things I add / remove frequently from the heap. I generally just create an object "and keep it". That usage works for me. Dirk -- http://dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org _______________________________________________ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel