Displaying 20 results from an estimated 30 matches for "r_makeexternalptr".
2007 Jul 25
6
Using R_MakeExternalPtr
...#39;m unsure how
to solve. I am looking to pass a C++ class object to R so that it may be
passed back to another C++ function later on to be used. I'm quite new to R
and this is my first time writing a package, so I hope you can bear with me.
The following is how I create the class and use R_MakeExternalPtr(). This
occurs in a function called "soamInit":
Session* sesPtr = conPtr->createSession(attributes);
void* temp = session;
SEXP out = R_MakeExternalPtr(temp, R_NilValue, R_NilValue);
return out;
The following is how I try to retrieve the class object in a different...
2005 Oct 24
2
R_MakeExternalPtr
Hi,
I'm using R_MakeExternalPtr() to store handles to (COM) objects in a SEXP.
The code basically is
sexp = R_MakeExternalPtr(handle,R_NilValue,R_NilValue);
R_RegisterCFinalizerEx(...);
After creating the sexp, LENGTH(sexp) returns some quite large integer
value. It seems like an "unitialized" value.
Can I safely...
2005 Apr 18
3
A 'true' R-wrapper for C++ classes
...An R-wrapper wraps the C-wrapper.
Here is a rough example to demonstrate the above:
---------------------
C++ class:
class foo
{
public:
foo();
~foo();
fun();
}
---------------------
C-wrapper:
extern "C" SEXP R_foo_init()
{
foo* obj = new foo();
SEXP res;
PROTECT(res = R_MakeExternalPtr(obj, R_NilValue, R_NilValue));
UNPROTECT(1);
return res;
}
extern "C" SEXP R_foo_fun(SEXP ptr)
{
foo *ptr = Hello
I am trying to wrap some C++ classes into R.
(1) Comparing the OOP and methods packages, I have came to this conclusion
that OOP works much better for this wrapper...
2011 Sep 14
2
External pointers and an apparent memory leak
...te_finalizer(SEXP eptr) {
Rprintf("Calling the finalizer\n");
void* vector = R_ExternalPtrAddr(eptr);
free(vector);
R_ClearExternalPtr(eptr);
}
SEXP h5R_allocate(SEXP size) {
int i = INTEGER(size)[0];
char* vector = (char*) malloc(i*sizeof(char));
SEXP e_ptr = R_MakeExternalPtr(vector, R_NilValue, R_NilValue);
R_RegisterCFinalizerEx(e_ptr, h5R_allocate_finalizer, TRUE);
return e_ptr;
}
If I run an R program like this:
v <- replicate(100000, {
.Call("h5R_allocate", as.integer(1000000))
})
rm(v)
gc()
Then you can see the problem (top reports that...
2008 Jan 29
2
Problem with new("externalptr")
...t;sexp_address", new("externalptr"))
[1] "0xde2ce0"
> .Call("sexp_address", new("externalptr"))
[1] "0xde2ce0"
Isn't that wrong?
I worked around this problem by writing the following C routine:
SEXP xp_new()
{
return R_MakeExternalPtr(NULL, R_NilValue, R_NilValue);
}
so I can create new "externalptr" instances from R with:
.Call("xp_new")
I understand that there is not much you can do from R with an "externalptr"
instance and that you will have to manipulate them at the C level anyway.
But si...
2017 Apr 14
2
potential bug in attribute handling for externalptr
Is the following expected behavior?
> mkext <- inline::cfunction(language="C", body='return
R_MakeExternalPtr(NULL, install("tag"), R_NilValue);')
> x <- mkext()
> y <- x
> attr(y, "foo") <- "bar"
> attributes(x)
$foo
[1] "bar"
I would expect that modifying y's attributes should not affect x.
[Tested on R version 3.3.3 x86_64-apple-...
2012 May 11
1
Passing externalptr to .C()
...example
getDeviceInfo <- function(handle) {
.C("groovyDevice_getDeviceInfo", PACKAGE="groovyDevice",
handle,
status = as.integer(0))[-1] # skip handle
}
where "handle" was the result of a .Call to a routine which
returned a SEXP which was the result of a R_MakeExternalPtr() call
The "c" routine looked like:
void
groovyDevice_getDeviceInfo(SEXP handle, int *status)
{
groovyHandle *gh = R_ExternalPtrAddr(handle);
*status = GroovyStatus(gh);
}
This all used to work fine. As of 2.15.0, I now get this:
Warning message:
In getDeviceInfo() :
passi...
2018 Mar 18
3
`@<-` modify its argument when slot is externalptr
...inconsistent behaviors of `@<-` operator when
used in different ways. Consider the following example:
library(inline)
# Function to generate an externalptr object with random address
new_extptr <- cfunction(c(), '
SEXP val = PROTECT(ScalarLogical(1));
SEXP out = PROTECT(R_MakeExternalPtr(&val, R_NilValue, val));
UNPROTECT(2);
return(out);
')
setClass("S4ClassHoldingExtptr", contains = "externalptr")
subs_extptr_1 <- function(x) {
x at .xData <- new_extptr()
x
}
subs_extptr_2 <- function(x) {
x <- `@<-`(x, &...
2016 May 14
2
R external pointer and GPU memory leak problem
...;
? ? ? ?PROTECT (input = AS_NUMERIC (input));
? ? ? ?double * temp;?
? ? ? ?temp = REAL(input);
double *x; ? ? ? ? ? ? ? ##here is the step which causes the memory leak
cudacall(cudaMalloc((void**)&x, *lenth * sizeof(double)));
//protect the R external pointer from finalizer
SEXP ext = PROTECT(R_MakeExternalPtr(x, R_NilValue, R_NilValue));
R_RegisterCFinalizerEx(ext, _finalizer, TRUE);
?
//copying CPU to GPU
cublascall(cublasSetVector(*lenth, sizeof(double), temp, 1,?
R_ExternalPtrAddr(ext), 1)); ? ?
? ? ? ?UNPROTECT(2);
return ext;
}
here is my finalized for my create function,
/*
define finalizer fo...
2016 Sep 15
1
Finalizer execution order question
Given an externalptr object 'pool' which protects an R object 'prot':
# SEXP prot = (a dynamically updated list with handles)
SEXP pool = R_MakeExternalPtr(p, R_NilValue, prot);
R_RegisterCFinalizerEx(pool, fin_pool, TRUE);
WRE explains that 'prot' remains in existence as long as 'pool' is
around. Does this also mean 'prot' still exists when the finalizer of
'pool' gets executed?
Long story: I am running into an iss...
2013 Dec 16
1
External pointers and changing SEXPTYPE
...ans_nms, R_z, R_a, R_b, R_c;
FTYPE *datafile;
char *fname;
float *a, *b, *c;
int f_type;
float t, p, l;
int st, na, result, bFlags;
XXX z;
} my_data_ptr;
// In a C function initializing the external pointer:
my_data_ptr *mydata = Calloc( 1, my_data_ptr ) ;
SEXP Rdata;
PROTECT(Rdata = R_MakeExternalPtr( mydata, R_fname, R_NilValue ));
...
mydata->a = Calloc(mydata->na, float);
// same for b and c
// initializing names so that I could use e.g. df$a where df is
returned by read_my_data()
PROTECT(mydata->ans_nms = Rf_allocVector(STRSXP, efldNR ));
for( ix = 0; ix < efldNR; ix++ )
S...
2007 Aug 03
2
How to properly finalize external pointers?
...Free(ptr);
Rprintf("finalized\n");
}else{
Rprintf("nothing to finalize\n");
}
return;
}
SEXP rindex_open(
SEXP Sn
){
int i,n = INTEGER(Sn)[0];
pINT ptr = Calloc(sizeof(INT)*n, INT);
SEXP extPtr, ret;
for (i=0;i<n;i++){
ptr[i] = i;
}
extPtr = R_MakeExternalPtr(ptr, install("Rindex_extPtr"), R_NilValue);
R_RegisterCFinalizer(extPtr, rindex_finalize);
PROTECT(ret = allocVector(VECSXP, 1));
SET_VECTOR_ELT(ret,0,extPtr);
UNPROTECT(1);
return ret;
}
SEXP rindex_close(
SEXP obj
){
int i, n= 10;
SEXP ret, extPtr=VECTOR_ELT(obj, 0);...
2024 Apr 25
1
Big speedup in install.packages() by re-using connections
...();
+}
+static void rollback_mhnd_sentinel(void* sentinel) {
+ // Failed to allocate memory while registering a finalizer,
+ // therefore must release the object
+ R_ReleaseObject((SEXP)sentinel);
+}
+static CURLM *get_mhnd(void)
+{
+ if (!mhnd_sentinel) {
+ SEXP sentinel = PROTECT(R_MakeExternalPtr(NULL, R_NilValue, R_NilValue));
+ R_PreserveObject(sentinel);
+ UNPROTECT(1);
+ // Avoid leaking the sentinel before setting the finalizer
+ RCNTXT cntxt;
+ begincontext(&cntxt, CTXT_CCODE, R_NilValue, R_BaseEnv, R_BaseEnv,
+ R_NilValue, R_NilValue);
+...
2005 Dec 09
3
external pointers
I have some C data I want to pass back to R opaquely, and then back to
C. I understand external pointers are the way to do so.
I'm trying to find how they interact with garbage collection and object
lifetime, and what I need to do so that the memory lives until the
calling R process ends.
Could anyone give me some pointers? I haven't found much documentation.
An earlier message
2018 Mar 18
0
`@<-` modify its argument when slot is externalptr
...t;-` operator when
> used in different ways. Consider the following example:
>
> library(inline)
>
> # Function to generate an externalptr object with random address
> new_extptr <- cfunction(c(), '
> SEXP val = PROTECT(ScalarLogical(1));
> SEXP out = PROTECT(R_MakeExternalPtr(&val, R_NilValue, val));
> UNPROTECT(2);
> return(out);
> ')
>
> setClass("S4ClassHoldingExtptr", contains = "externalptr")
>
> subs_extptr_1 <- function(x) {
> x at .xData <- new_extptr()
> x
> }
>
> subs_extptr...
2001 Mar 01
0
Finalization and external pointer objects
...ore difficult and it's likely to be a significant amount of
work.
I'm guessing that there needs to be some of the following functions at
the R level - perhaps they are already around and I've missed them.
GenExtPointer - basically the same thing that can get returned by a
.Call using R_MakeExternalPtr. The address of the appropriate part of
this would then need to be passed in the place of "output" above,
presumably by using an R interface to R_ExternalPtrAddr.
register.finalizer mentioned in Lukes discussion papers is needed.
I welcome any advice about other ways of approaching this...
2004 Oct 30
1
Destructive str(...)?
...avior intentional, undocumented or simply wrong?
Cheers,
Simon
[Tested with R 2.0.0 release (2004-10-04) on Mac OS X 10.3.5 - I have
currently no other machine to test it on, but I very much suspect that
this is platform-independent.]
the C code used to generate the object:
SEXP class, sref = R_MakeExternalPtr((void*) obj, R_NilValue,
R_NilValue);
PROTECT(class = allocVector(STRSXP, 1));
SET_STRING_ELT(class, 0, mkChar("ObjCid"));
SET_CLASS(sref, class);
UNPROTECT(1);
2014 Nov 12
2
How to maintain memory in R extension
Hello,
I wrote a system to perform data analysis in C++. Now I am integrating
it to R. I need to allocate memory for my own C++ data structures,
which can't be represented by any R data structures. I create a global
hashtable to keep a reference to the C++ data structures. Whenever I
allocate one, I register it in the hashtable and return its key to the
R code. So later on, the R code can
2007 Jul 10
1
How to preserve data across function calls in a library package
Hi,
I am writing an R package with two functions in C++. So far
everything works.
Now, i would like to write a third function which would use a pointer
(it is a pointer to a class object) created by first function.
I tried placing this pointer outside of the function definitions
(i.e to make it global) but when called in the 3rd function i get
> *** caught bus error ***
address 0x0,
2007 Jul 10
1
How to preserve data across function calls in a library package
Hi,
I am writing an R package with two functions in C++. So far
everything works.
Now, i would like to write a third function which would use a pointer
(it is a pointer to a class object) created by first function.
I tried placing this pointer outside of the function definitions
(i.e to make it global) but when called in the 3rd function i get
> *** caught bus error ***
address 0x0,