[R] not safe to return vector pointer

Thomas Lumley tlumley at u.washington.edu
Mon Sep 10 22:24:45 CEST 2001


On Mon, 10 Sep 2001, Chris Jeffery wrote:

>
> Hello All,
>
> I recently upgraded from R-1.1.1 to R-1.2.2.

If it was recent, it would have been better to update to R-1.3.0 or 1.3.1

> I have an R function that uses .Call() to
> return a list from C code.  The C code
> has the form:
>
> SEXP function(SEXP var)
> {
>    SEXP rlist ;
>    PROTECT(rlist = NEW_LIST(3)) ;
>    VECTOR_PTR(rlist)[0] = NEW_INTEGER(1) ;
>    VECTOR_PTR(rlist)[1] = NEW_STRING(1) ;
>
>    ...
>
>    UNPROTECT(1) ;
>
>    return(rlist) ;
> }
>
> When I try to .Call("function",...) from R I get
> the following error message:
>
> Error: not safe to return vector pointer
>
> Why is it no longer safe to return a vector pointer?
> How can I correct this problem?

The new memory manager in R 1.2 needs to know when objects are updated.
This means that there are new macros for access to strings and vectors.

You need to use eg

  SET_VECTOR_ELT(rlist,0,NEW_INTEGER(1));

instead of

  VECTOR(rlist)[0]=NEW_INTEGER(1));.

There are similar macros for string access.

More detail is given in the 'Writing R Extensions' manual. Modifying code
to handle this new `write barrier' isn't that painful and the new memory
manager is much better than the old one, so it was worth the change.


	-thomas

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list