Protection stack overflow with the C interface for R

Viewed 49

I'm currently writing a R package for Delaunay and Voronoï tessellations in 3D. Most of the implementation is done in C (with the help of the Qhull library).

While running the Delaunay triangulation on a large number of points (> 10000), I received this error:

Error: protect(): protection stack overflow

In my C code, I run UNPROTECT at the end of the functions, only one time. I'm wondering: if I split the code in blocks, to run UNPROTECT multiple times instead of one time (e.g. each time I reach 1000 PROTECT), could it change something? Is there a known limit for the number of protections?

1 Answers

I finally removed the PROTECTs in the for loops:

  PROTECT(R_vertices = allocVector(VECSXP, nsites));
  PROTECT(vnames = allocVector(STRSXP, nsites));
  nprotect += 2;
  for(unsigned i = 0; i < nsites; i++) {
    SEXP vertex = SiteSXP(vertices[i], dim);
    //    SEXP vertex;
    //    PROTECT(vertex = SiteSXP(vertices[i], dim));
    //    nprotect++;
    SET_VECTOR_ELT(R_vertices, i, vertex);
    SET_STRING_ELT(vnames, i, Rf_asChar(VECTOR_ELT(vertex, 0)));
  }
  setAttrib(R_vertices, R_NamesSymbol, vnames);

It seems they are not necessary. I don't output the vertexs at the end, I only store them in R_vertices.

Related