Skip to content

Q: Is it safe to mess with option 'cpp11_preserve_xptr'? #268

Closed
@HenrikBengtsson

Description

@HenrikBengtsson

While trying to troubleshoot some segfaults, I stumbled upon the R option cpp11_preserve_xptr that cpp11 uses internally;

private:
// The preserved list singleton is stored in a XPtr within an R global option.
//
// It is not constructed as a static variable directly since many
// translation units may be compiled, resulting in unrelated instances of each
// static variable.
//
// We cannot store it in the cpp11 namespace, as cpp11 likely will not be loaded by
// packages.
// We cannot store it in R's global environment, as that is against CRAN
// policies.
// We instead store it as an XPtr in the global options, which avoids issues
// both copying and serializing.
static SEXP get_preserve_xptr_addr() {
static SEXP preserve_xptr_sym = Rf_install("cpp11_preserve_xptr");
SEXP preserve_xptr = Rf_GetOption1(preserve_xptr_sym);
if (TYPEOF(preserve_xptr) != EXTPTRSXP) {
return R_NilValue;
}
auto addr = R_ExternalPtrAddr(preserve_xptr);
if (addr == nullptr) {
return R_NilValue;
}
return static_cast<SEXP>(addr);
}

To be clear, I have no idea what it does, but looking at it's value, e.g.

> getOption("cpp11_preserve_xptr")
<pointer: 0x55555a020518>

is see it's an external pointer.

My question is, is the cpp11 code robust to anything that messes with this option? For example, can it safely be unset:

options(cpp11_preserve_xptr = NULL)

at any time? Can you imagine a scenario where changing it's value would wreak havoc?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions