Skip to content

Commit 9ec6602

Browse files
committed
HACK: just define preserve_list in the global env
1 parent e3aa704 commit 9ec6602

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

inst/include/cpp11/protect.hpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include "R_ext/Utils.h" // for R_CheckUserInterrupt
1515
#include "Rversion.h" // for R_VERSION, R_Version
1616

17+
#include <iostream>
18+
1719
#if defined(R_VERSION) && R_VERSION >= R_Version(3, 5, 0)
1820
#define HAS_UNWIND_PROTECT
1921
#endif
@@ -288,8 +290,18 @@ static struct {
288290
// The .preserve_list singleton is a member of cpp11::: and is managed by the R
289291
// runtime. It cannot be constructed a header since many translation units may be
290292
// compiled, resulting in unrelated instances of each static variable.
291-
SEXP ns = safe[Rf_findVarInFrame](R_NamespaceRegistry, safe[Rf_install]("cpp11"));
292-
list_singleton = safe[Rf_findVar](safe[Rf_install](".preserve_list"), ns);
293+
294+
// FIXME how can we create the cpp11 namespace when it doesn't already exist?
295+
SEXP list_singleton_sym = safe[Rf_install](".cpp11_preserve_list");
296+
297+
list_singleton = safe[Rf_findVarInFrame](R_GlobalEnv, list_singleton_sym);
298+
299+
if (list_singleton == R_UnboundValue) {
300+
list_singleton = PROTECT(Rf_cons(R_NilValue, R_NilValue));
301+
R_PreserveObject(list_singleton);
302+
UNPROTECT(1);
303+
safe[Rf_defineVar](list_singleton_sym, list_singleton, R_GlobalEnv);
304+
}
293305
}
294306

295307
return list_singleton;

inst/include/cpp11/r_vector.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,9 @@ inline r_vector<T>::r_vector(const r_vector<T>& rhs)
766766

767767
template <typename T>
768768
inline r_vector<T>::r_vector(r_vector<T>&& rhs)
769-
: cpp11::r_vector<T>(rhs), protect_(preserved.insert(data_)), capacity_(rhs.capacity_) {
769+
: cpp11::r_vector<T>(rhs),
770+
protect_(preserved.insert(data_)),
771+
capacity_(rhs.capacity_) {
770772
rhs.data_ = R_NilValue;
771773
rhs.protect_ = R_NilValue;
772774
}

0 commit comments

Comments
 (0)