R/C++ interfaces: crashes when using .c(), followed by correct results when R restarted
First, the posting guide asks you to send questions about compiled code to the R-devel list. These are all classic symptoms of the use of uninitialized variables or writing out of bounds, but of which you can catch by using valgrind: see 'Writing R Extensions'. They are much harder to find on Windows (which although you did not tell us your OS, is what this looks like).
On 20/09/2012 10:19, Franckx Laurent wrote:
Dear all
I have written a function in C++ , equil_distC, that I am calling from an R script.
In the last few days, R has repeatedly crashed when calling this function, or delivered obviously wrong outputs. However, when I restarted R after the crash, the results turned out to be OK most of the time although I had not modified the C++ code.
Although the code runs correctly now, I am not sure why it does work right now, and didn't just an hour ago.
Let me summarize the main steps I undertook:
* After having modified the script with the C++ code, I compiled it using R CMD SHLIB mylatestversion.cpp - no problems occurred then.
* Next, I loaded the dll into R with
dyn.load("C:\\Ccodefortransortmodel\\ mylatestversion.dll")
* Next, I called equil_distC with
testres2 <- .C("equil_distC", as.integer(rowmaxfiets),as.integer(colmaxfiets), as.double(nodes), as.integer(links_dist_calc), as.integer(cyclepathsfirstline), as.integer(numzones), as.integer(numnodes), as.integer(numlinks), result = double(6))
* About half of the time, whenever I had changed the C++ code, the results were either nonsensical or R crashed.
* When R did not crash, I unloaded the dll with:
dyn.unload("C:\\Ccodefortransortmodel\\equildistCforodpair_2012_09_20v8.dll")
* Next, I deleted the .dll and the .o of the most recent version, I modified the C++ code and I ran the above procedure again.
* In most cases when R crashed, a new run of the procedure above resulted in correct results, even though the C++ code was not modified.
I have also noticed that, in several cases, apparently irrelevant changes to the code would lead to a crash. This occurred for instance when I replaced %d with %f in Rprintf()) - my first conjecture was actually that this was the root of the problem, but the code runs correctly now with %f.
My own conjecture is that, some way or another, old versions of equil_distC kept on circulating in memory.
For instance, even after a dyn.unload of all previous versions of the dll, "is.loaded("equil_distC")" would still lead to a TRUE.
I would like to understand the structural reason for these problems. Again, the problem is not the C++ code - it does run correctly now, although it had crashed in the previous run.
Best regards
Laurent Franckx, PhD
VITO NV
Boeretang 200, 2400 MOL, Belgium
Tel. + 32 14 33 58 22
Skype: laurent.franckx
laurent.franckx at vito.be
Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595