From simon.urbanek at r-project.org  Wed Jul 25 17:54:14 2007
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Wed, 25 Jul 2007 11:54:14 -0400
Subject: [Rd] Using R_MakeExternalPtr
In-Reply-To: <11785023.post@talk.nabble.com>
References: <11785023.post@talk.nabble.com>
Message-ID: <5947C8C0-9BD0-4DE0-9D2E-8E67BD4918C0@r-project.org>

Jon,

I suspect that you're leaving out some important details - please  
include your R code for inspection as the C++ code below seems ok  
(although you may want to show us the declarations as well). Also you  
may want to look into this with a debugger in case the segfault is  
actually in your C++ code due to some memory management issue.

Cheers,
Simon

On Jul 25, 2007, at 11:35 AM, 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
> -- 
> View this message in context: http://www.nabble.com/Using- 
> R_MakeExternalPtr-tf4142904.html#a11785023
> Sent from the R devel mailing list archive at Nabble.com.
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>



[Rd] Using R_MakeExternalPtr
Simon Urbanek