From murdoch at stats.uwo.ca  Wed Mar  7 13:34:09 2007
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Wed, 07 Mar 2007 07:34:09 -0500
Subject: [Rd] Garbage collector crashes after calling a C function
In-Reply-To: <50805D6FBD91904D86AF433349315E420AC759@ICOSRVCORREO01.ICO.SCS.local>
References: <50805D6FBD91904D86AF433349315E420AC759@ICOSRVCORREO01.ICO.SCS.local>
Message-ID: <45EEB141.1070506@stats.uwo.ca>

On 3/7/2007 6:42 AM, Sole Acha, Xavi wrote:
> Dear listers,
> 
>  
> 
> a few days ago I asked a question about a problem I had with a C function programmed by myself to be called from R. Thanks to your help, I have been able to look a bit further into it, so now I can be a bit more specific when telling what happens.
> 
>  
> 
> The fact is that, when my C function finishes its execution (after calling it via the ".C" interface), R seems ok but then crashes when the garbage collector executes, giving a segmentation fault.
> 
>  
> 
> As some of you suggested, I have executed my function with valgrind, and I get no memory errors, but some memory leaks (which I am not able to locate although compiling my program with the -c flag), and I also get the memory error from RunGenCollect: (which I suppose is causes the segmentation fault). You can see a small piece of the valgrind output below
> 
>  
> 
> Using gctorture to make the garbage collector crash inside the execution of my program didn't work (my program finished). The question is: what are the typical programming errors that can make R garbage collector crash? Could it be because of these memory leaks? If so, how could I trace them? I have tried it, but running my program with gdb is not an easy task to do, as it performs a huge number of iterations.
> 
>  
> 
> Thank you very much in advance for your help. I would appreciate any hint that could help me solve this tricky problem.

The obvious guess is that you're writing out of bounds in some array. 
Remember to explicitly set the type of arrays you pass in to .C (e.g. 
as.double(1:10) is different from 1:10), and remember that R uses 
1-based indexing (from 1 to 10 in the case above) whereas C uses 0 based 
indexing (from 0 to 9).

I don't know what sort of tests valgrind does and why it doesn't notice 
an error in your code; it doesn't run in Windows, as far as I know.

Duncan Murdoch



[Rd] Garbage collector crashes after calling a C function
Duncan Murdoch