[R] Run destructors of local C++ objects on SWIG_fail

Arrange that destructors of local C++ objects in the wrapper function
get run on SWIG_fail (which calls Rf_error() which calls longjmp()).

We achieve this by putting almost everything in the function in its
own block, and end that right before Rf_error() at which point those
destructors will get called.
This commit is contained in:
Olly Betts 2022-10-14 13:18:56 +13:00 committed by Olly Betts
commit 5f96d15943
3 changed files with 25 additions and 1 deletions

View file

@ -1970,6 +1970,13 @@ int R::functionWrapper(Node *n) {
}
Printv(f->def, ")\n{\n", NIL);
// SWIG_fail in R leads to a call to Rf_error() which calls longjmp()
// which means the destructors of any live function-local C++ objects won't
// get run. To avoid this happening, we wrap almost everything in the
// function in a block, and end that right before Rf_error() at which
// point those destructors will get called.
if (CPlusPlus) Append(f->def, "{\n");
Printv(sfun->def, ")\n{\n", NIL);
@ -2123,6 +2130,7 @@ int R::functionWrapper(Node *n) {
if (need_cleanup) {
Printv(f->code, cleanup, NIL);
}
if (CPlusPlus) Append(f->code, "}\n");
Printv(f->code, " Rf_error(\"%s %s\", SWIG_ErrorType(SWIG_lasterror_code), SWIG_lasterror_msg);\n", NIL);
Printv(f->code, " return R_NilValue;\n", NIL);
Delete(cleanup);