Broom
Broom

Reputation: 51

Rcpp Error Null value passed as symbol address

I am new to Rcpp.

I created an rcpp function which takes a dataframe with 2 columns and a vector as input, and returns a vector. My data are as below

set.seed(10)
min= sort(rnorm(1000,800,sd=0.1))
max= min+0.02
k=data.frame(min,max)
explist= sort(rnorm(100,800,sd=0.2))

Then I call the cfilter.cpp

k$output <- cfilter(k,explist)

#include <Rcpp.h> 
using namespace Rcpp;
// [[Rcpp::export]]
CharacterVector cfilter(DataFrame k, NumericVector explist) {

  NumericVector col1 = k["min"];
  NumericVector col2 = k["max"];
  NumericVector exp = explist ;

  int n = col1.size();
  int j = 0;
  CharacterVector out(n);

  for (int i=0; i<n ; i++){
    out[i]=NA_STRING;

    while(exp[j]<= col2[i]){

        if( exp[j]>= col1[i] && exp[j]<= col2[i] ){
          out[i]="Y";
          break;
        }
        else if(exp[j]>col2[i]){

          break;
        }
        else {
          j++ ;
        }
    }

  }
 return out;
}

It run perfectly fine for 16171 times I called it. And then suddenly, in the loop 16172 it just stops with an error:

> myfile$output<- cfilter(k,explist2)
Error in .Call(<pointer: (nil)>, k, explist) : 
  NULL value passed as symbol address

I checked k and explist for NA values but there aren't any, there is no problem whatsoever with the input. I have no clue how to fix this and what causes this error.

Thanks in advance for any response

Upvotes: 5

Views: 2932

Answers (1)

Mehmet Aydemir
Mehmet Aydemir

Reputation: 41

I came across the same problem. I'm not an Rcpp expert, nor C++ nor, a backend coding expert. I have circumvented this problem by re-sourcing my cpp file every time I want to make a call of the function. So, for example if following is your for loop:

for(i in 1:SampleSize){
   out[[I]]<-cfilter(k,explist)
 }

Do something like:

for(i in 1:SampleSize){
   sourceCpp("cfilter.cpp")
   out[[i]]<-cfilter(k,explist)
}

Again, I don't know exactly why this worked for me, but it worked. Based on my shallow knowledge of C++, it might be related to memory allocation and that every time you source, memory is released and hence there is no mis-allocation. But I think this is a very wild guess. Best

Upvotes: 1

Related Questions