<html><head><meta http-equiv="Content-Type" content="text/html charset=iso-8859-1"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>It seems to me that XPtr is like an ownership pointer. It is much like unique_ptr. What you need here is a non-owning reference pointer, like weak_ptr.</div><div><br></div>Have you tried<div><br></div><div>XPtr<A> A::getPtr(){<br> return(XPtr<A>(this, false));<br>}</div><div><br></div><div>With the optional argument, a finalizer is not set and the gc will not delete the C++ object. I haven't used XPtr in this way, but from the document I guess this shall work.</div><div><br></div><div>Best,</div><div><br></div><div>Yan Zhou</div><div><br><div><div>On Feb 11, 2013, at 6:55 PM, Michael Shvartsman <<a href="mailto:mshvarts@umich.edu">mshvarts@umich.edu</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">
<meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type">
<div bgcolor="#FFFFFF" text="#000000">Hi Dirk -- <br>
<br>
Thanks for the quick response. My design is (I think) textbook
aggregation -- in my case, A is a logger class and B is an experiment
class. The experiment calls methods from the logger, but the logger may
be active for more than one experiment so it can't be created inside it.
So I create the logger and pass a pointer to it into the experiment.
There are definitely other ways to design this, but this way doesn't
seem to be particularly unreasonable. <br>
<br>
The basic goal is to create a C++ object on the R side (in this
case, the logger class) and pass it back down to the C++ side for later
use by another C++ object (the experiment class). I thought that XPtr
was the right way to do this (based on e.g.
<a class="moz-txt-link-freetext" href="http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2011-December/003214.html">http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2011-December/003214.html</a>),
but I could be misunderstanding either XPtrs or the example in that
email. If there is a different way to pass down the Rcpp class and
unwrap it to arrive at a pointer to the underlying C++ class, it would
be great. Best, <br>
<br>
Mike. <br>
<blockquote style="border: 0px none;" cite="mid:20761.11552.254969.963327@max.nulle.part" type="cite">
<div style="margin:30px 25px 10px 25px;" class="__pbConvHr"><div style="display:table;width:100%;border-top:1px solid
#EDEEF0;padding-top:5px"> <div style="display:table-cell;vertical-align:middle;padding-right:6px;"><img photoaddress="edd@debian.org" photoname="Dirk Eddelbuettel" name="postbox-contact.jpg" height="25px" width="25px" id="3bc19a85-6899-4122-80b8-6b6b4f53d2ea" src="cid:part1.03060508.02080507@umich.edu"></div> <div style="display:table-cell;white-space:nowrap;vertical-align:middle;width:100%">
<a moz-do-not-send="true" href="mailto:edd@debian.org" style="color:#737F92
!important;padding-right:6px;font-weight:bold;text-decoration:none
!important;">Dirk Eddelbuettel</a></div> <div style="display:table-cell;white-space:nowrap;vertical-align:middle;">
<font color="#9FA2A5"><span style="padding-left:6px">February 11, 2013
12:40 PM</span></font></div></div></div>
<div style="color:#888888;margin-left:24px;margin-right:24px;" __pbrmquotes="true" class="__pbConvBody">On 11 February 2013 at
12:16, Michael Shvartsman wrote:<br>| Hi Rcpp-devel -<br>| <br>| In
some of my code, I return an XPtr pointing to one C++ class <br>| (e.g.
class A) to the R side, and then pass it back down to the C++ side <br>|
in the constructor of another class (e.g. class B, which needs access
to <br>| some methods in class A). If I rm() both class A and the
pointer to it <br>| on the R side (or they otherwise both go out of
scope), then the garbage <br>| collector calls the destructor of class A
twice and attempts to free the <br>| memory twice.<br>| <br>|
Happens on OSX and Ubuntu with latest R and Rcpp. Is this intended <br>|
behavior? If yes, what's a recommended workaround? I can go into more <br>|
detail on why this is useful to my workflow if needed. Below is a <br>|
minimal example. Best,<br><br>Thanks for sending a complete example --
very helpful.<br><br>I will think about this some more, but my first gut
reaction is that your<br>design is wrong. You are essentially "just"
using XPtr to get two references<br>to the same memory, and then for all
intends and purposes doing<br><br> x = new Something;<br> delete
x;<br> delete x;<br><br>which also doesn't work. I have worked quite
happily with XPtr in the past,<br>and so have others. I have however
not mixed them with Modules, I think, so<br>maybe that enters.<br><br>But
I think the best answers to the "I hurts when I do this" remains "well<br>then
just don't it". <br><br>And I may well have missed something here...<br><br>Dirk<br><br>|
<br>| Mike Shvartsman.<br>| <br>| -----<br>| library(Rcpp)<br>|
library(inline)<br>| <br>| inc <- '<br>| using namespace Rcpp;<br>|
class A{<br>| public:<br>| A();<br>| ~A();<br>|
XPtr<A> getPtr();<br>| };<br>| <br>| XPtr<A>
A::getPtr(){<br>| return(XPtr<A>(this));<br>| }<br>| <br>|
A::A(){<br>| Rcout << "CTOR" << std::endl;<br>| }<br>| <br>|
A::~A(){<br>| Rcout << "DTOR" << std::endl;<br>| }<br>|
<br>| RCPP_MODULE(mod){<br>| class_<A>("A")<br>|
.constructor()<br>| .method("getPtr", &A::getPtr)<br>| ;<br>|
}<br>| '<br>| <br>| func <- cxxfunction(signature(), include=inc,
plugin='Rcpp')<br>| <br>| mod <- Module('mod', getDynLib(func))<br>| <br>|
<br>| A <- mod$A<br>| <br>| a <- new(A)<br>| aPtr <-
a$getPtr()<br>| rm(a)<br>| rm(aPtr)<br>| gc()<br>| -----<br>| <br>|
_______________________________________________<br>| Rcpp-devel mailing
list<br>| <a class="moz-txt-link-abbreviated" href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>|
<a class="moz-txt-link-freetext" href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br><br></div>
<div style="margin:30px 25px 10px 25px;" class="__pbConvHr"><div style="display:table;width:100%;border-top:1px solid
#EDEEF0;padding-top:5px"> <div style="display:table-cell;vertical-align:middle;padding-right:6px;"><img photoaddress="mshvarts@umich.edu" photoname="Michael Shvartsman" name="compose-unknown-contact.jpg" height="25px" width="25px" id="b8ba222d-0e26-4c6b-aac7-cdc58c2fc545" src="cid:part2.08080002.06010306@umich.edu"></div> <div style="display:table-cell;white-space:nowrap;vertical-align:middle;width:100%">
<a moz-do-not-send="true" href="mailto:mshvarts@umich.edu" style="color:#737F92
!important;padding-right:6px;font-weight:bold;text-decoration:none
!important;">Michael Shvartsman</a></div> <div style="display:table-cell;white-space:nowrap;vertical-align:middle;">
<font color="#9FA2A5"><span style="padding-left:6px">February 11, 2013
12:16 PM</span></font></div></div></div>
<div style="color:#888888;margin-left:24px;margin-right:24px;" __pbrmquotes="true" class="__pbConvBody">Hi Rcpp-devel -
<br>
<br> In some of my code, I return an XPtr pointing to one C++ class
(e.g. class A) to the R side, and then pass it back down to the C++ side
in the constructor of another class (e.g. class B, which needs access to
some methods in class A). If I rm() both class A and the pointer to it
on the R side (or they otherwise both go out of scope), then the garbage
collector calls the destructor of class A twice and attempts to free the
memory twice.
<br>
<br> Happens on OSX and Ubuntu with latest R and Rcpp. Is this
intended
behavior? If yes, what's a recommended workaround? I can go into more
detail on why this is useful to my workflow if needed. Below is a
minimal example. Best,
<br>
<br> Mike Shvartsman.
<br>
<br>-----
<br>library(Rcpp)
<br>library(inline)
<br>
<br>inc <- '
<br>using namespace Rcpp;
<br>class A{
<br> public:
<br> A();
<br> ~A();
<br> XPtr<A> getPtr();
<br>};
<br>
<br>XPtr<A> A::getPtr(){
<br> return(XPtr<A>(this));
<br>}
<br>
<br>A::A(){
<br> Rcout << "CTOR" << std::endl;
<br>}
<br>
<br>A::~A(){
<br> Rcout << "DTOR" << std::endl;
<br>}
<br>
<br>RCPP_MODULE(mod){
<br> class_<A>("A")
<br> .constructor()
<br> .method("getPtr", &A::getPtr)
<br> ;
<br>}
<br>'
<br>
<br>func <- cxxfunction(signature(), include=inc, plugin='Rcpp')
<br>
<br>mod <- Module('mod', getDynLib(func))
<br>
<br>
<br>A <- mod$A
<br>
<br>a <- new(A)
<br>aPtr <- a$getPtr()
<br>rm(a)
<br>rm(aPtr)
<br>gc()
<br>-----
<br>
<br></div>
</blockquote>
</div>
_______________________________________________<br>Rcpp-devel mailing list<br><a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</blockquote></div><br></div></body></html>