0

I guess this may be a general question on debug. I created function using Rcpp. The compiler did not spot any error, so I can use the function. However, when running the function, with around 1/100 probability, the R just stops and closes itself without any details. I am doing simulation so the input data could be different for each time. So I am guessing may be there is some special situation that will give error. My question is do we have any way to know what line cause such error?

So far what I am using is to using comment out some components to see which part create such error. It is OK so far as long as I can replicate the error. But It is not working this time (I cannot replicate the error, may be it is too specific.) Any comments or suggestions are highly appreciated.

Mahali Sindy
  • 111
  • 2
  • 2
    Hi and welcome to SO. For this problem, information on your operating system is crucial. Things are very different when you use Windows, Linux or Mac. Use the edit button to add that info to your question. Meanwhile, check the following (possibly duplicate) questions. https://stackoverflow.com/questions/53622354/how-to-debug-line-by-line-rcpp-generated-code-in-windows https://stackoverflow.com/questions/21226337/what-are-productive-ways-to-debug-rcpp-compiled-code-loaded-in-r-on-os-x-maveri – Joris Meys Jan 16 '19 at 13:42
  • 2
    PS: It might be that moderators point out your question is essentially a duplicate of a previous one. This will close your question, but that doesn't mean it's a bad one. It just means it's already answered. Kind regards – Joris Meys Jan 16 '19 at 13:43
  • 3
    R is probably crashing out because of a segmentation fault or other process violation. Use a debugger to figure out where. Often its your code trying to access memory outside its allowed bounds, but could be a zillion other reasons. – Spacedman Jan 16 '19 at 14:07
  • 1
    Another (older) duplicate: https://stackoverflow.com/questions/11345537/debugging-line-by-line-of-rcpp-generated-dll-under-windows – Dirk Eddelbuettel Jan 16 '19 at 14:24

1 Answers1

2

R crashed because of a segmentation fault i.e. you tried to access to a memory you can't access. This typically happens when you try to access the n+1 element of a vector that only have n elements or when you badly used pointers. To spot non obvious segfault (but not only) I always use Valgrind. Write a minimal R code that generates a segfault. Then run it from a terminal like that.

R -d "valgrind --tool=memcheck --leak-check=full" --vanilla -f test.R

To install valgrind on GNU/Linux sudo apt-get install valgrind. Valgrind does not exist on windows.

JRR
  • 3,024
  • 2
  • 13
  • 37
  • Could you provide a short explanation of what that command is doing, and maybe a sample output? I would appreciate it, thanks! – thc Feb 09 '19 at 00:16