问题
I've written some Rcpp code to find intersections in two piecewise linear curves. I approached the problems in the naive way requiring nested loops (checking all pairs of segments), so went to Rcpp for speed. (The problem is pictorially described in "Example 1" here: https://prezi.com/rwkji_leuwdr/r-and-c-integration/ )
The code compiles fine with sourceCpp, and even runs fine and gives expected results. BUT, the strange issue is that it consistently crashes RStudio either after running it multiple times (say, 5 or 6), or a little while after running it. I haven't seen anyone else document this sort of problem in a google/stack overflow search. I'm new to C++ and don't have any kind of intuition as to where the problem is coming from; seems like pretty weird behavior to me given that the functions run as desired at first.
Code and data are here: https://github.com/suztolwinskiward/ReproducepRoblem
Thanks in advance to anyone who helps!
回答1:
FWIW, I had the same problem and the bug seemed to be that I was trying to allocate too much data to a vector. It might be helpful to use the ()
operator rather than the []
operator in Rcpp. With []
my code would compile and crash after I ran it a few times; with ()
the code will compile but it'll throw an error at runtime.
来源:https://stackoverflow.com/questions/33768796/rstudio-crashes-after-calling-an-rcpp-function-multiple-times