Reputation: 1253
When terminating early with Rcpp::stop()
, is it necessary to unprotect early in the below example?
I'm aware that I could have rewritten this using IntegerVector without the protect and unprotect calls, but I suppose I'm asking this for my own understanding as the function produces no stack imbalance warning with or without the first UNPROTECT
.
library(Rcpp)
cppFunction('SEXP with_protect(){
SEXP out = PROTECT(Rf_allocVector(INTSXP, 10));
int *p_out = INTEGER(out);
if (true){
UNPROTECT(1);
Rcpp::stop("early termination");
}
p_out[0] = 10;
UNPROTECT(1);
return out;
}')
cppFunction('SEXP without_protect(){
SEXP out = PROTECT(Rf_allocVector(INTSXP, 10));
int *p_out = INTEGER(out);
if (true){
Rcpp::stop("early termination");
}
UNPROTECT(1);
return out;
}')
with_protect()
#> Error in eval(expr, envir, enclos): early termination
without_protect()
#> Error in eval(expr, envir, enclos): early termination
Created on 2023-10-27 with reprex v2.0.2
Upvotes: 0
Views: 68