Skip to content

Commit c9d405e

Browse files
committed
check for new RCPP_USE_FINALIZE_ON_EXIT to switch XPtr's default behaviour on exit
1 parent 1abe59a commit c9d405e

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

ChangeLog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2021-09-29 Iñaki Ucar <[email protected]>
2+
3+
* inst/include/Rcpp/XPtr.h: Check for new define
4+
`RCPP_USE_FINALIZE_ON_EXIT` to flip the value of XPtr's `finalizeOnExit`
5+
parameter from false (default) to true.
6+
* inst/tinytest/test_xptr.R: Added test for this functionality.
7+
18
2021-09-27 Dirk Eddelbuettel <[email protected]>
29

310
* README.md: Added total downloads badge

inst/include/Rcpp/XPtr.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// XPtr.h: Rcpp R/C++ interface class library -- smart external pointers
33
//
44
// Copyright (C) 2009 - 2020 Dirk Eddelbuettel and Romain Francois
5+
// Copyright (C) 2021 Dirk Eddelbuettel, Romain Francois and Iñaki Ucar
56
//
67
// This file is part of Rcpp.
78
//
@@ -51,7 +52,11 @@ template <
5152
typename T,
5253
template <class> class StoragePolicy = PreserveStorage,
5354
void Finalizer(T*) = standard_delete_finalizer<T>,
54-
bool finalizeOnExit = false
55+
#ifdef RCPP_USE_FINALIZE_ON_EXIT
56+
bool finalizeOnExit = true
57+
#else
58+
bool finalizeOnExit = false
59+
#endif
5560
>
5661
class XPtr :
5762
public StoragePolicy< XPtr<T,StoragePolicy, Finalizer, finalizeOnExit> >,

inst/tinytest/test_xptr.R

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

2-
## Copyright (C) 2009 - 2019 Dirk Eddelbuettel and Romain Francois
2+
## Copyright (C) 2009 - 2020 Dirk Eddelbuettel and Romain Francois
3+
## Copyright (C) 2021 Dirk Eddelbuettel, Romain Francois and Iñaki Ucar
34
##
45
## This file is part of Rcpp.
56
##
@@ -35,3 +36,31 @@ expect_true(xptr_release(xp), info = "check release of external pointer")
3536
expect_true(xptr_access_released(xp), info = "check access of released external pointer")
3637

3738
expect_error(xptr_use_released(xp), info = "check exception on use of released external pointer")
39+
40+
# test finalizeOnExit default depending on RCPP_USE_FINALIZE_ON_EXIT
41+
file_path <- tempfile(fileext=".cpp")
42+
on.exit(unlink(file_path), add=TRUE)
43+
R <- shQuote(file.path(R.home(component = "bin"), "R"))
44+
cmd <- paste0(R, " -s -e 'Rcpp::sourceCpp(\"", file_path, "\"); test()'")
45+
46+
code <- '
47+
#include <Rcpp.h>
48+
using namespace Rcpp;
49+
50+
template <typename T>
51+
void custom_finalizer(T* obj) {
52+
Rcout << "custom_finalizer was called" << std::endl;
53+
delete obj;
54+
}
55+
56+
// [[Rcpp::export]]
57+
void test() {
58+
XPtr<int, PreserveStorage, custom_finalizer> x(new int);
59+
}
60+
'
61+
62+
writeLines(code, file_path)
63+
expect_silent(system(cmd), info="check that finalizer is NOT called on exit")
64+
65+
writeLines(c("#define RCPP_USE_FINALIZE_ON_EXIT", code), file_path)
66+
expect_stdout(system(cmd), info="check that finalizer is called on exit")

0 commit comments

Comments
 (0)