Closed coolbutuseless closed 2 years ago
Yeah this is really not needed. For example in the case below, you're not calling any R API's in between your allocation and unprotect, so there really no chance of an accidental gc here.
SEXP res = PROTECT(Rf_allocVector(RAWSXP, buf.size));
memcpy(RAW(res), buf.buf, buf.size);
free(buf.buf);
UNPROTECT(1);
Also if this was a problem, the cran rchck
tool would have raised a warning about it....
Ah. OK. I thought the RAW() macro would be something to PROTECT() against - it's one of the examples on the R extensions page and the emphasis on "assume any macro can allocate".
Happy to leave it as your call.
RAW in this case just gives us the pointer to the underlying buffer that we just allocated. So it's really redundant. But thanks for the suggestion :)
R extension guide recommends erring on the side of caution with PROTECT/UNPROTECT - so I've ensured PROTECT() used for all allocVector(), and delayed UNPROTECT() until just prior to return().
This may be overboard. Feel free to ignore if I'm being too conservative here.