[Rd] Using R_MakeExternalPtr

Hin-Tak Leung hin-tak.leung at cimr.cam.ac.uk
Wed Jul 25 18:40:35 CEST 2007

As other has commented, without the rest/full of your code it is 
difficult to tell... I have a few suggestions though:

- it is obviously a memory-related issue. An external pointer is partly
allocated by R and partly allocated by you - you need to check both 
parts, and their individual usages.

- I think the part allocated by R is correct; you might need a few 
PROTECT() somewhere unrelated in "soamSubmit" as well, when you are 
playing R objects in C/C++... what I mean is, the external pointer
may be unrelated to your problem.

Jonathan Zhou wrote:
> Hi all, 
> I've been writing a package and I've run into a problem that I'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 C++
> function called "soamSubmit", where sesCon is the externalPtr : 
> 	void* temp = R_ExternalPtrAddr(sesCon);
>         Session* sesPtr = reinterpret_cast<Session*>(temp);
> The error I get when trying to run the R function is : 
>  *** caught segfault ***
> address 0x3, cause 'memory not mapped'
> Traceback:
>  1: .Call("soamSubmit", counter, sesCon, final.script, packages)
>  2: soam.Rapply(x, tester, join.method = c, njobs = 2)
> So it seems like a scoping problem to me, though I'm unsure how to solve it.  
> -Jon

More information about the R-devel mailing list