<div dir="ltr">I'm pretty sure I saw this post on StackOverflow before. Please don't cross post, or at least indicate that you are when you do so. It's also nice to give credit based on the advice you receive...<div><br></div><div>The main issue in your code, I think, is here:</div><div><br></div><div><div>struct Element : public std::enable_shared_from_this< Element > {</div><div>  SEXP key_;</div><div>  </div><div>  /* Simple constructor that assigns the key. */</div><div>  Element(SEXP key) : key_(key) {</div><div>    PROTECT(key_);</div><div>  }</div><div>  </div><div>  ~Element() {</div><div>    UNPROTECT_PTR(key_);</div><div>  }</div><div>  </div><div>  /* Convert the object to a R object. */</div><div>  operator SEXP() const;</div><div>};</div><div><br></div><div>Using PROTECT here is the wrong thing for SEXP class members -- PROTECT uses the R stack-based protection scheme, and that will not work well for objects whose lifetimes extend beyond the scope of when they're created. You likely need `R_PreserveObject` and `R_ReleaseObject`. Note that this is how Rcpp vectors ensure they are protected from the garbage collector.</div><div><br></div><div>Kevin</div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Oct 6, 2016 at 9:31 AM, 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:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
Hi Anton,<br>
<br>
Thanks for posting here.<br>
<span class="gmail-"><br>
On 6 October 2016 at 10:52, Anton Bossenbroek wrote:<br>
| I want to add a large number of objects in C++ that are managed by `shared_ptr` in a `vector`. However, when I push the limits of the amount that I want to allocate the data in R becomes inconsistent.<br>
<br>
</span>A bit of background.<br>
<br>
Rcpp exists because of the excellent idea (by Dominick, around 2004 to 2005)<br>
that using C++ templates for SEXP++ would make interfacing QuantLib easier in<br>
the RQuantLib package I had started a few years earlier.<br>
<br>
QuantLib is a marvel. It started in the early 2000s and was the first project<br>
I saw which has bet "big" on Boost testing, and, very importantly,<br>
shared_ptr.  QuantLib also makes extensive use of SWIG to govern interfaces<br>
to (a large number of) other languages.  And here is the key: None of those<br>
can use shared_ptr objects.  Neither does RQuantLib. Even though they are<br>
pervasive in QuantLib.<br>
<br>
Now, we are here to interface R.  All that Rcpp does, in essence, is making<br>
it trivially easy to get objects in and out of R as proxy objects:<br>
lightweight, no copy.  And the memory _is still managed by R_. So that you<br>
never need to use a SEXP directly in C++.  That is a feature.  [ Of course,<br>
sometimes you need the SEXP as a quasi-variant type, but that is not the<br>
common use. So let's just call it 'no SEXP in user code'. ]<br>
<br>
So I am not claiming that what you want to do here is impossible -- but I am<br>
not aware of another project having done this: hold R objects in a smart_ptr.<br>
<br>
Also, you haven't really explained what you want to do, what the life cyle of your<br>
objects is etc pp.<br>
<br>
At the simplest level, Rcpp use is synchronous:  you call from R, pass some<br>
content, do some calculations, report something back -- and everything<br>
intermediary is destroyed.  You can persist things, of course, when you<br>
manage the memory.  But you need to be very sure the things are then away<br>
from R -- copies are probably best. XPtr objects are used in that context.<br>
<br>
Anyway, end of short speech.  I would recommend you try to break you project<br>
into smaller, more fundamental tasks.  Maybe we can help with those, one by one.<br>
<span class="gmail-HOEnZb"><font color="#888888"><br>
Dirk<br>
<br>
--<br>
<a href="http://dirk.eddelbuettel.com" rel="noreferrer" target="_blank">http://dirk.eddelbuettel.com</a> | @eddelbuettel | <a href="mailto:edd@debian.org">edd@debian.org</a><br>
</font></span><div class="gmail-HOEnZb"><div class="gmail-h5">______________________________<wbr>_________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-<wbr>project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" rel="noreferrer" target="_blank">https://lists.r-forge.r-<wbr>project.org/cgi-bin/mailman/<wbr>listinfo/rcpp-devel</a><br>
</div></div></blockquote></div><br></div></div></div>