Skip to content

Convert SEXP to XPtr cause lost of tag value #968

@Marlin-Na

Description

@Marlin-Na

Here is a minimal example:

#include <Rcpp.h>
using namespace Rcpp;

typedef struct {
    int x;
    int y;
} mycomplex;

void myfinalizer(mycomplex* x) {
    free(x);
}

typedef XPtr<mycomplex, Rcpp::PreserveStorage, myfinalizer> XPtrMyComplex;

// [[Rcpp::export]]
SEXP init(int a, int b) {
    mycomplex* m = (mycomplex*) malloc(sizeof(mycomplex));
    m->x = a;
    m->y = b;
    XPtrMyComplex ans = XPtrMyComplex(m, true, Rf_ScalarRaw(42), R_NilValue);
    return ans;
}

// [[Rcpp::export]]
int access(SEXP robj) {
    XPtrMyComplex obj = as<XPtrMyComplex>(robj);
    mycomplex* p = obj.checked_get();
    return p->x + p->y;
}


/*** R
library(xptr)
x <- init(3, 4)
print(xptr_tag(x))
access(x)
print(xptr_tag(x))
*/

I get:

R> library(xptr)

R> x <- init(3, 4)

R> print(xptr_tag(x))
[1] 2a

R> access(x)
[1] 7

R> print(xptr_tag(x))
NULL

I am not sure if this behavior is expected. Also, I would appreciate if anyone can explain to me the semantics when doing this conversion: is a new external pointer object allocated or it is just wrapper around the original one?

Thanks!

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