<div dir="ltr">The idea is indeed to avoid copying memory. I thought that doing something like the following would allow me to read the values created in R from within the threads:<div><br></div><div>Rcpp::NumericVector vec(vector_from_R);</div><div>std::vector<RcppParallel::RVector<double>> series;</div><div>series.push_back(RcppParallel::RVector<double>(vec));</div><div>// then in the threads:</div><div>double val = series[index_for_this_thread][0];</div><div><br></div><div>The data created on the R side is never modified by these functions, just read. It is possible for different threads to read the same memory, but I thought reading was not subject to race conditions.</div><div><br></div><div>The segfaults are very consistent, every OSX build fails with the same error at the same point. The fact that it happens with clang++ but not with gcc++ is puzzling to me.</div><div><br></div><div>The Rcpp::List may contain a lot of NumericVector or NumetricMatrix series, so I would rather not copy all of them.</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jan 20, 2018 at 2:46 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"><span class=""><br>
On 20 January 2018 at 12:45, Alexis Sarda wrote:<br>
| I've found out that the problem remains on OSX builds, and apparently it is<br>
| caused by clang itself. I used R-hub's fedora-clang-devel to test:<br>
|<br>
| <a href="https://artifacts.r-hub.io/dtwclust_5.1.0.9000.tar.gz-6f452fd6aeea4307921df2ab2337e6bb/dtwclust.Rcheck/00check.log" rel="noreferrer" target="_blank">https://artifacts.r-hub.io/<wbr>dtwclust_5.1.0.9000.tar.gz-<wbr>6f452fd6aeea4307921df2ab2337e6<wbr>bb/dtwclust.Rcheck/00check.log</a><br>
|<br>
| The error that stands out to me is:<br>
|<br>
| *** Error in `/opt/R-devel/lib64/R/bin/<wbr>exec/R': corrupted<br>
| double-linked list: 0x00000000099a3870 ***<br>
|<br>
|<br>
| I am essentially doing a parallel distance matrix calculation as shown in<br>
| the Rcpp gallery, but I have several distance functions. All the classes<br>
| that provide distance calculations have a member wrapping std::vector of<br>
| either RcppParallel's RVector<double>, RMatrix<double>, or Armadillo's<br>
| cx_vec. Here's the template I'm using to wrap those members:<br>
|<br>
| <a href="https://github.com/asardaes/dtwclust/blob/master/src/utils/TSTSList.h" rel="noreferrer" target="_blank">https://github.com/asardaes/<wbr>dtwclust/blob/master/src/<wbr>utils/TSTSList.h</a><br>
|<br>
| Could the corruption be caused by this?<br>
<br>
</span>It looks to me like you are just moving _actual Rcpp vectors_ around from the<br>
Rcpp::List into your container, and then access them using your operator<br>
types.  But ... that still accesses R memory through these vectors, and with<br>
that we may get a (rare ?) race condition on stack unwinding etc.<br>
<br>
The truly paranoid approach would be to actually make truly distinct types<br>
and copy (ie memcpy). That file is short, so maybe you can try it.<br>
<div class="HOEnZb"><div class="h5"><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>
</div></div></blockquote></div><br></div>