Garmaine Staff asked 2 years ago

During an implementation of cpp library for R, I'm encountered a strange crash of R session.

I compiled and tested the library in ubuntu with gcc compiler and the function in object works well. Then I compiled the same code in mac OS catalina with clang and the problem arise: R session crash during the function execution.

After some debugger I discovered that the function still works if a factory object inside it is removed. Moreover the crash happens when the C++ routine ends. Note that the library is compiled with c++ 11.

I report a sketch of my code:

# in R scrips:
my_R_function <- function(par1, par2){
   Output_CPP <- my_function(par1, par2)
return output_CPP}

SEXP my_function(SEXP Rpar1, Rpar2){
    double my_par1 = conversion(Rpar1);
    double my_par2 = conversion(Rpar2);

    std::unique_ptr<solver_calss<T>> my_solver = SOLVERfactory<T>::createMYsolver(my_par1, my_par2);

   my_solver->apply_method();

   std::vector<double my_solution = my_solver->getSolution();
   SEXP result = R_NilValue;
   result = PROTECT(Rf_allocVector(REAL, 1));
   SET_VECTOR_ELT(result, 0, Rf_allocVector(REALSXP, my_solution.size()));

    double *rans = REAL(VECTOR_ELT(result, 0));
    for(int i = 0; i < solution.size(); i++)
    {
        rans[i] = solution[i];
    }

  UNPROTECT(1);  
  return result;
}