[Rcpp-devel] Segfaults for a function returning list of vectors of indices

Dirk Eddelbuettel edd at debian.org
Tue Feb 28 21:14:52 CET 2012


On 28 February 2012 at 13:52, Douglas Bates wrote:
| On Tue, Feb 28, 2012 at 1:22 PM, Dirk Eddelbuettel <edd at debian.org> wrote:
| >
| > On 28 February 2012 at 10:54, Chris DuBois wrote:
| > | Hi all,
| > |
| > | Given a vector of integers x = sample(1:K,N,replace=TRUE) with N > K,
| > | I want to return a list where element k is a vector of indices i where
| > | x[i] = k.  (If there is already an R function for this, please let me
| > | know!)  Here is the Rcpp code:
| > |
| > | // xr: vector of integers
| > | // mr: max
| > | superwhich <- cxxfunction(signature(xr="integer",Kr="integer"),
| > | '
| > | int K = as<int>(Kr);
| > | Rcpp::IntegerVector x(xr);
| > |
| > | // Initialize vector of vectors
| > | std::vector< std::vector<int> > xx;
| > | for (int i = 0; i < K; i++) {
| > |   std::vector<int> tmp(0);
| > |   xx.push_back(tmp);
| > | }
| > |
| > | // Push onto appropriate vector for each element of x
| > | for (int i = 0; i < x.size(); i++) {
| > |   xx[x[i]].push_back(i);
| > | }
| > |   return wrap(xx);
| > | ', plugin="Rcpp")
| > |
| > | For example:
| > | > x <- c(1,3,3,1)
| > | > superwhich(x,4)
| > | [[1]]
| > | integer(0)
| > |
| > | [[2]]
| > | [1] 0 3
| > |
| > | [[3]]
| > | integer(0)
| > |
| > | [[4]]
| > | [1] 1 2
| > |
| > |
| > | So with large vectors and large values of K, I occasionally get
| > | segfaults.  I believe I have thoroughly checked that I am not making
| >
| > Second one today of that flavour :-/
| >
| > | indexing errors.  I believe I am running into garbage collection
| > | issues.  Isn't wrap() good enough to make R objects?  I could use
| > | Rcpp::List from the start, but is there an equivalent of push_back for
| > | Rcpp::IntegerVectors?  (I've read the documentation, and I apologize
| > | in advance if I am missing something obvious.)
| >
| > I do not think you do.  "Occassionally" things seem to go astray on large
| > problems. I don't usually get hit by this.
| >
| > Doug Bates however had issues with RcppEigen and has been using memory
| > debuggers like valgrind.  The issue is that those are somewhat hard to use,
| > slow everything down and maybe demand on idea of the underlying code.
| > That said, you can also "just run" with valgrind and see if it reports
| > something.
| 
| I don't think it was RcppEigen that was causing troubles.  Are you

My bad. Didn't mean to imply that. Mean to say that while working on
RcppEigen you suffered greatly because of a bug you then fixed.

| thinking of tracking down the failure to protect the expr argument to
| the Rcpp::Evaluator::run?

Yes.
 
| Anyway, if you are willing to put up with slower execution while you
| are debugging and have valgrind installed you can use
| 
| R CMD BATCH --vanilla -d valgrind myBatchRFileName.R

Great, thanks.

Dirk

-- 
"Outside of a dog, a book is a man's best friend. Inside of a dog, it is too
dark to read." -- Groucho Marx


More information about the Rcpp-devel mailing list