Crash when calling Rcpp::Function asMatrix("as.matrix")
If I build the following Rcpp code in a new project, then run the R code below, I crash the R session. I've tested this on Windows 7 64-bit and Windows 10 64-bit with R versions 3.2.1 and 3.2.3.
I tested on several recent versions of Rcpp, including Rcpp_0.12.9.
#include<Rcpp.h>
// [[Rcpp::export]]
Rcpp::NumericMatrix crash(const SEXP& x){
Rcpp::Function asMatrix("as.matrix");
return asMatrix(x);
}
When I vary n below, crashes starts to occur whenn >= 70000 (approximately). Below this value of n, the crash function returns the expected result. The for loop below is present because the crash may occur at different values of i, generally not until i > 5 on my machine, but always when i < 100.
n = 100000
a = 1:n
for(i in 1:100) oops = crash(a)
Wrapping 1:n in data.frame causes crashes as well.
for(i in 1:100) oops = crash(data.frame(a))
However, if I wrap 1:n with as.matrix there are no crashes at any level of n (subject to memory limitations, of course).
for(i in 1:100) oops = crash(as.matrix(a))
Huh?
You need me to clarify anything above?
I don't see the point of this. Why call the as.matrix() R function? Matrices are perfectly well created via Rcpp::NumericMatrix. Next, what makes you think a namespace rather explicitly dubbed internal was one for you to call?
Sure, have a segfault. You can even have two. I just don;t think I am interested. I think you are just wasting your time, and also mine.
This below is not an Rcpp internal function (see above example). I made note of the internal call when trying to understand the crash using below (commented out but perhaps not good to label the issue in the title).
Do you expect the below to segfault based on my example? Rcpp::Function asMatrix("as.matrix");
R> library(Rcpp)
R> cppFunction("void foo() { Rcpp::Function asMatrix(\"as.matrix\"); } ")
R> foo()
R>
R>
You never answered my question of why you need this in the first place. There is a better idiom.
I appreciate you taking the time to prepare a bug report, I am simply disagreeing with you over whether this report matters or not. As far as I can tell you use Rcpp in a somewhat non-standard way. You break it, you get to keep the pieces. Sorry.
Suppose I want to create an Rcpp function that accepts some input object x that, in R, is either a numeric vector, matrix or data.frame (or others, e.g. xts). It seemed reasonable in the Rcpp function to accept x as an SEXP object, then convert x to a NumericMatrix in the body of the function (similar to what we might do in an R function with as.matrix(x)) to "standardize" the input. Creating an Rcpp::Function object that calls into R's as.matrix function works, except when the SEXP x object contains more than some critical number of elements, at which point there is a segfault. That seemed peculiar to me, so there you have it. If you choose to ignore the issue above, ok then, it's your package. I have my own packages to maintain and I get it.
Here is a more useful version of your code, including R code:
#include<Rcpp.h>
// [[Rcpp::export]]
Rcpp::NumericMatrix crash(const SEXP& x){
// this is how we would create a matrix from a SEXP
// return Rcpp::NumericMatrix(x);
Rcpp::Function asMatrix("as.matrix");
return asMatrix(x);
}
/*** R
n <- 100000
a <- 1:n # this is integer vector
for (i in 1:100) oops = crash(a)
for(i in 1:100) oops = crash(data.frame(a))
cat("Still here\n")
*/
When we source that, nothing happens:
R> library(Rcpp)
R> sourceCpp("/tmp/nonbug.cpp")
R> n <- 100000
R> a <- 1:n # this is integer vector
R> for (i in 1:100) oops = crash(a)
R> for(i in 1:100) oops = crash(data.frame(a))
R> cat("Still here\n")
Still here
R>
Thanks for the follow up, much appreciated. I'll have to check on a Linux box to see if there's different behavior. Are you on a Debian distro?
The below segfaults on Windows every time I run it, now I've tested on three different machines across two OS versions (Windows 7 and 10).
library(Rcpp)
cppFunction("NumericMatrix foo(const SEXP& x) { Rcpp::Function asMatrix(\"as.matrix\"); return asMatrix(x); } ")
for(i in 1:100) x = foo(1:100000)
Ubuntu 16.10. I don't think it matters.
You are forcing memory reallocations by the way you are doing this. C++ is not R.
I think you simply need to rethink your design: you are throwing 1:n (which is an IntegerVector after all) or a data.frame() at as.matrix() which is simply not all that reasonable---even if it may be the R level.
Sure, in a perfect world we'd never have segfaults either. But you really are starting from the wrong foot methinks.
On Ubuntu 16.04 (and a box with more ram, just in case):
R> library(Rcpp)
R> cppFunction("NumericMatrix foo(const SEXP& x) { Rcpp::Function asMatrix(\"as.matrix\"); return asMatrix(x); } ")
R> for(i in 1:100) x = foo(1:100000)
R>
On my 16.10 machine with rather laughably little ram:
R> library(Rcpp)
R> cppFunction("NumericMatrix foo(const SEXP& x) { Rcpp::Function asMatrix(\"as.matrix\"); return asMatrix(x); } ")
R> for(i in 1:100) x = foo(1:100000)
R>
I do agree completely with your comments. I'm just hoping (or was hoping) to find the proper design pattern in Rcpp C++ code to allow a numeric vector, matrix or data.frame as possible inputs to the same function argument, then convert any of the 3 cases to a NumericMatrix. Is that possible in an idiomatic way? This would also include integer objects of the same 3 cases as well. I know it may be a bridge too far, but if anyone knows, it's you.
FWIW, I don't see an issue running that code on my Windows 7 VM either (R 3.3.2, Rcpp 0.12.9).
Just double checking, but have you by any chance overridden the as.matrix function with your own definition in the global environment, or something similar?
It is not trivial. Esp data.frame() is just very different; a list of vectors. Whereas a matrix is just one vector with a dimension attribute.
We have some posts at SO and the Rcpp Gallery on the basic switching over SEXP -- but that is often simpler (ie int vs double vs complex vector, say).
@kevinushey no overridden as.matrix
@eddelbuettel good tips, thanks.