diff --git a/inst/include/Rcpp/macros/macros.h b/inst/include/Rcpp/macros/macros.h index edc50b2e8..76fd5be54 100644 --- a/inst/include/Rcpp/macros/macros.h +++ b/inst/include/Rcpp/macros/macros.h @@ -29,9 +29,11 @@ #ifndef BEGIN_RCPP #define BEGIN_RCPP \ int rcpp_output_type = 0 ; \ + int nprot = 0; \ (void)rcpp_output_type; \ SEXP rcpp_output_condition = R_NilValue ; \ (void)rcpp_output_condition; \ + static SEXP stop_sym = Rf_install("stop"); \ try { #endif @@ -48,26 +50,30 @@ catch(Rcpp::exception& __ex__) { \ rcpp_output_type = 2 ; \ rcpp_output_condition = PROTECT(rcpp_exception_to_r_condition(__ex__)) ; \ + ++nprot; \ } \ catch( std::exception& __ex__ ){ \ rcpp_output_type = 2 ; \ rcpp_output_condition = PROTECT(exception_to_r_condition(__ex__)) ; \ + ++nprot; \ } \ catch( ... ){ \ rcpp_output_type = 2 ; \ rcpp_output_condition = PROTECT(string_to_try_error("c++ exception (unknown reason)")) ; \ + ++nprot; \ } \ if( rcpp_output_type == 1 ){ \ Rf_onintr() ; \ } \ if( rcpp_output_type == 2 ){ \ - SEXP stop_sym = Rf_install( "stop" ) ; \ SEXP expr = PROTECT( Rf_lang2( stop_sym , rcpp_output_condition ) ) ; \ - Rf_eval( expr, R_GlobalEnv ) ; \ + ++nprot; \ + Rf_eval( expr, R_BaseEnv ) ; \ } \ if (rcpp_output_type == 3) { \ Rcpp::internal::resumeJump(rcpp_output_condition); \ - } + } \ + UNPROTECT(nprot); #endif #ifndef END_RCPP