Skip to content

Commit 0cf1aaf

Browse files
rework macros BEGIN_RCPP and END_RCPP to avoid leaks. #286
1 parent 44bae9c commit 0cf1aaf

File tree

3 files changed

+157
-13
lines changed

3 files changed

+157
-13
lines changed

inst/include/Rcpp/macros/macros.h

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// macros.h: Rcpp R/C++ interface class library -- Rcpp macros
44
//
5-
// Copyright (C) 2012 Dirk Eddelbuettel and Romain Francois
5+
// Copyright (C) 2012 - 2015 Dirk Eddelbuettel and Romain Francois
66
//
77
// This file is part of Rcpp.
88
//
@@ -27,21 +27,33 @@
2727
#define RCPP_GET_CLASS(x) Rf_getAttrib(x, R_ClassSymbol)
2828

2929
#ifndef BEGIN_RCPP
30-
#define BEGIN_RCPP try {
30+
#define BEGIN_RCPP \
31+
int rcpp_output_type = 0 ; \
32+
SEXP rcpp_output_condition = R_NilValue ; \
33+
try {
3134
#endif
3235

3336
#ifndef VOID_END_RCPP
34-
#define VOID_END_RCPP \
35-
} \
36-
catch (Rcpp::internal::InterruptedException &__ex__) { \
37-
Rf_onintr(); \
38-
} \
39-
catch (std::exception &__ex__) { \
40-
forward_exception_to_r(__ex__); \
41-
} \
42-
catch (...) { \
43-
::Rf_error("c++ exception (unknown reason)"); \
44-
}
37+
#define VOID_END_RCPP \
38+
} \
39+
catch( Rcpp::internal::InterruptedException &__ex__) { \
40+
rcpp_output_type = 1 ; \
41+
} \
42+
catch( std::exception& __ex__ ){ \
43+
rcpp_output_type = 2 ; \
44+
rcpp_output_condition = PROTECT(exception_to_r_condition(__ex__)) ; \
45+
} catch( ... ){ \
46+
rcpp_output_type = 2 ; \
47+
rcpp_output_condition = PROTECT(string_to_try_error("c++ exception (unknown reason)")) ; \
48+
} \
49+
if( rcpp_output_type == 1 ){ \
50+
Rf_onintr() ; \
51+
} \
52+
if( rcpp_output_type == 2 ){ \
53+
SEXP stop_sym = Rf_install( "stop" ) ; \
54+
SEXP expr = PROTECT( Rf_lang2( stop_sym , rcpp_output_condition ) ) ; \
55+
Rf_eval( expr, R_GlobalEnv ) ; \
56+
}
4557
#endif
4658

4759
#ifndef END_RCPP

0 commit comments

Comments
 (0)