I am using the two R packages 'tidyverse' and 'Rcpp' to execute an C++-function within 'mutate' used on a tibble object.
I get the following error:
Error in mutate_impl(.data, dots) : 
  Evaluation error: GC encountered a node (0x112b8d800) with an unknown SEXP type: FREESXP at memory.c:1013. 
I tried to use valgrind on it, but valgrind gives me an error without even executing and I somehow can't get this fixed on my computer. So I would like to ask, if other people get the same error and might have a solution to it.
Here is an example code to be executed:
# load necessary packages
library( tidyverse )
library( Rcpp )
# define C++ function inline
cppFunction( '
             IntegerVector lee_ready_vector( NumericVector & price, NumericVector &bidprice, 
                                NumericVector &askprice ) {
               const int nrows = price.length();
               IntegerVector indicator( nrows );
               if ( nrows < 3 ) {
                 return indicator;
               }
               if ( nrows != bidprice.length() || nrows != askprice.length() ) {
                 throw std::invalid_argument( "Arguments differ in lengths" );
             }
             NumericVector midprice = ( askprice + bidprice ) / 2.0;
             try {
               for( int i = 2; i <= nrows; ++i ) {
                 if ( price[i] == askprice[i] ) {
                   indicator[i] = 1;
                 } else if ( price[i] == bidprice[i] ) {
                   indicator[i] = -1;
                 } else {
                   if ( price[i] > midprice[i] ) {
                     indicator[i] = 1;
                   } else if ( price[i] < midprice[i] ) {
                     indicator[i] = -1;
                   } else { 
                   /* price == midpice */
                       if ( price[i] > price[i-1] ) {
                         indicator[i] = 1;
                       } else if ( price[i] < price[i-1] ) {
                         indicator[i] = -1;
                       } else {
                         if ( price[i] > price[i-2] ) {
                           indicator[i] = 1;
                       } else {
                           indicator[i] = -1;
                       }
                     }
                   }
                 }
               }
             } catch ( std::exception &ex ) {
               forward_exception_to_r( ex );
             } catch (...) {
               ::Rf_error( "c++ exception (unknown reason)" );
             }
             return indicator;
             }')
# define function for random dates inline
latemail <- function( N, st="2012/01/01", et="2012/03/31" ) {
  st <- as.POSIXct( as.Date( st ) )
  et <- as.POSIXct( as.Date( et ) )
  dt <- as.numeric( difftime( et,st,unit="sec" ) )
  ev <- sort(runif( N, 0, dt ) )
  rt <- st + ev
  sort( as.Date( rt ) )
}
# set random seed 
set.seed( 12345 )
# start test loop
# try 100 times to crash the session
# repeat this whole loop several times, if necessary
for ( i in 1:100 ) {
  # 500,000 observation altogether
  N <- 500000
  dates <- latemail( N )
  mid <- sample(seq(from=8.7, to=9.1, by = 0.01), N, TRUE)
  # bid and ask series lay around mid series
  bid <- mid - .1
  ask <- mid + .1
  # p is either equal to bid or ask or lays in the middle
  p <- rep( 0, N )
  for(i in 1:2000) {
    p[i] <- sample( c(mid[i], bid[i], ask[i]), 1 )
  }
  # create the dataset
  df <- tibble( dates, p, bid, ask )
  # execute the C++ function on grouped data
  df %>% group_by( dates ) %>% 
    mutate( ind = lee_ready_vector( p, bid, ask ) ) %>% 
    ungroup()
}  
Is anybody able to reproduce the error. Anyone able to give a solution?
 
    