<html><head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type">
</head><body bgcolor="#FFFFFF" text="#000000"><span>Yes, sorry for not
being as quick with the follow-up, works exactly as described and
perfect for my purposes. <br>
<br>
I now wonder whether I could've done this with RCPP_EXPOSED_CLASS
without creating and passing around the XPtr (at least, the module in
this example
<a class="moz-txt-link-freetext" href="http://romainfrancois.blog.free.fr/index.php?post/2012/10/25/Rcpp-modules-more-flexible">http://romainfrancois.blog.free.fr/index.php?post/2012/10/25/Rcpp-modules-more-flexible</a>
accepts and returns a pointer to a class without explicit as/wrap-ing
to XPtrs). But I'm quite happy with the solution Yan Zhou provided.
Thanks again, <br>
<br>
Mike. <br>
</span><br>
<blockquote style="border: 0px none;"
cite="mid:20761.46457.805876.134344@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"
src="cid:part1.03050509.09030002@umich.edu" name="postbox-contact.jpg"
height="25px" width="25px"></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
10:22 PM</span></font></div></div></div>
<div style="color:#888888;margin-left:24px;margin-right:24px;"
__pbrmquotes="true" class="__pbConvBody"><div>On 11 February 2013 at
19:10, Yan Zhou wrote:<br>| It seems to me that XPtr is like an
ownership pointer. It is much like<br>| unique_ptr. What you need here
is a non-owning reference pointer, like<br>| weak_ptr.<br>| <br>| Have
you tried<br>| <br>| XPtr<A> A::getPtr(){<br>|
return(XPtr<A>(this, false));<br>| }<br>| <br>| With the optional
argument, a finalizer is not set and the gc will not delete<br>| the C++
object. I haven't used XPtr in this way, but from the document I guess<br>|
this shall work.<br><br>Nice work. Should have thought of the finalizer
-- on quick testing this does<br>indeed do the trick. Well done.<br><br>Dirk<br><br></div></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="zhouyan@me.com" photoname="Yan Zhou"
src="cid:part2.07020707.02010905@umich.edu"
name="compose-unknown-contact.jpg" height="25px" width="25px"></div> <div
style="display:table-cell;white-space:nowrap;vertical-align:middle;width:100%">
<a moz-do-not-send="true" href="mailto:zhouyan@me.com"
style="color:#737F92
!important;padding-right:6px;font-weight:bold;text-decoration:none
!important;">Yan Zhou</a></div> <div
style="display:table-cell;white-space:nowrap;vertical-align:middle;">
<font color="#9FA2A5"><span style="padding-left:6px">February 11, 2013
2:10 PM</span></font></div></div></div>
<div style="color:#888888;margin-left:24px;margin-right:24px;"
__pbrmquotes="true" class="__pbConvBody"><meta content="text/html;
charset=ISO-8859-1" http-equiv="Content-Type"><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><br
class="Apple-interchange-newline"></div><br></div></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"
src="cid:part2.07020707.02010905@umich.edu"
name="compose-unknown-contact.jpg" height="25px" width="25px"></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
1:55 PM</span></font></div></div></div>
<div style="color:#888888;margin-left:24px;margin-right:24px;"
__pbrmquotes="true" class="__pbConvBody">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
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 moz-do-not-send="true"
href="http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2011-December/003214.html"
class="moz-txt-link-freetext">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>
</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="edd@debian.org" photoname="Dirk Eddelbuettel"
src="cid:part1.03050509.09030002@umich.edu" name="postbox-contact.jpg"
height="25px" width="25px"></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"><div>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>
<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"
src="cid:part2.07020707.02010905@umich.edu"
name="compose-unknown-contact.jpg" height="25px" width="25px"></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>
</body></html>