[R] R crashing after successfully running compiled code

Duncan Murdoch murdoch.duncan at gmail.com
Wed Oct 31 13:00:17 CET 2012

On 12-10-30 11:13 PM, Adam Clark wrote:
> I'm running R 2.15.1x64, though the same problem persists for 2.13.0x32 and
> 2.13.0x64.
> I am trying to run compiled C code using the .C convention. The
> code compiles without problems, dynamically loads within the R
> workspace with no problems, and even runs and gives correct results with
> no problems.
> However, R will randomly crash within a few minutes of successfully using
> the compiled function.
> For example, if I run my compiled function using:
> dyn.load("mycfun.dll")
> answer<-.C("mycfun", parameters...), I get a completely sensible
> result that gets stored to "answer".
> However, if I try to do too many things to "answer", the R exits
> without warning.
> I've tried dyn.unload in hopes that R would become stable afterwards, but
> in this case using the function crashes R without fail.
> Usually, I can either plot, or view, or save "answer" to a file - but never
> take more than a single action before R exits. This does not appear to
> depend on how long R has been open. Initially, I thought it was a bug in
> the "inline" function, but I'm finding the same problem now that I'm using
> the dynamically loaded file directly. I'm used to R being insanely stable,
> and am somewhat mystified by this whole problem.
> My next move is to learn the ".Call" convention, as I suspect that
> my problem is related to my "C" function using memory that R doesn't
> know is used. But - before I invest a while lot more time on this, I'd
> like to know whether anybody things this is likely to solve the problem.
> If not, I may just want to run my code entirely in C, and forget the
> R problem.

I think your C code has a bug in it.  The bug might go away when you 
rewrite the function to work within the .Call convention, but it is 
probably easier to find the bug and fix it with the current code.

Things to look for:

Are you fully allocating all arrays in R before passing them to C?  The 
C code receives a pointer and will happily write to it, whether that 
makes sense or not.

Are you careful with your limits on vectors?  In R, a vector is indexed 
from 1 to n, but the same vector in C is indexed from 0 to n-1.  If the 
C code writes to entry n, that will eventually cause problems.

Are you allocating memory in your C code?  There are several ways to do 
that, depending on how you want it managed.  If you do it one way and 
expect it to be managed in a different way, you'll get problems.

Duncan Murdoch

More information about the R-help mailing list