Fix memory leak in for loop

∥☆過路亽.° 提交于 2020-12-15 04:28:43

问题


I have issues with memory leaks on a C++ function that I have sourced into R through Rcpp. This result in a sudden abortion of my R session. Sometimes I do get the following error message:

GC encountered a node (0x111e25820) with an unknown SEXP type: 11 at memory.c:1748

What is weird to me is that if in function_test I move double lower =1; and double upper = value2; inside the for loop and edited them by also adding 'i': double lower =1 + i; and double upper = value2 + i; the function works without aborting my R session.

The code below is sourced in R using the function sourceCpp from Rcpp R package. I run the function with the following values:

R function:

function_test(value1=400, value2=400, alpha = 0.5772, beta=25.13274, gamma=5e-04)

C++ code sourced in R:

    // [[Rcpp::depends(RcppEigen)]]
// [[Rcpp::depends(RcppNumerical)]]
#define EIGEN_PERMANENTLY_DISABLE_STUPID_WARNINGS
#include <Eigen/Eigen>
#include <RcppNumerical.h>
#include <Rcpp.h>
using namespace Rcpp;
using namespace Numer;

// [[Rcpp::export]]
double other_func(double i, double t, double beta) {

  int j = i - 3;

  NumericVector x(j);
  double total = 0;

  if( j == 0){
  total = 0;
  }else{

  for(int u = 0; u <= (j - 1); u++) {
  int res = pow(u+1, 2.0);
  x[u] = res;
  }

  NumericVector::iterator it;
  for(it = x.begin(); it != x.end(); ++it) {
  total += *it;
  }

  }

  double log_term = pow(log(t - (0.5 + i - 2.0)), (i-1));
  double int_tot = (total / t);

  double exp_term = exp(-int_tot);
  double add_terms = (i * exp_term * log_term) / pow(beta,(i-1));

return add_terms;

  }


// [[Rcpp::export]]
double func1(double t, double alpha, double beta, double gamma, double delta) {


    double term1 = ( 2.0 * log(t - 0.5) + (2.0 * alpha)) / beta;

    double N;
    if (t == 1 || t == 2) {
    N = 2;
    }else{
    if (t <= 44) {
    N = t;
    }else{
    N = 44;
    }
    }

    NumericVector term_up(N);
    term_up[0] = 0;
    term_up[1] = term1;
    NumericVector term_sign(N);
    term_sign[0] = 0;
    term_sign[1] = 1;

    for(int i = 3; i <= N; ++i) {
    term_up[i-1] = other_func(i,  t, beta);
    if ( i % 2 == 0){
    term_sign[i-1] = 1;
    }else{
    term_sign[i-1] = -1;
    }
    }

    int size = term_up.size();
    double res = 0;
    for(int i=0;i<size;i++){
    res += term_sign[i] * term_up[i];
    }



    double int_final = (1/(beta*t)) * (1 - ((1/(1+(1/beta))) * (res)));
    double final = int_final * exp(-2.0 * gamma * (delta + t));

    return final;
}

class Func1PDF: public Func
{
private:
   double alpha;
   double beta;
   double gamma;
   double delta;
public:
    Func1PDF(double alpha_, double beta_, double gamma_, double delta_) : alpha(alpha_), beta(beta_),  gamma(gamma_),  delta(delta_) {}

    double operator()(const double& t) const{
        return func1(t, alpha, beta, gamma, delta);
    }
  };

  // [[Rcpp::export]]
  double integrate_func(double lower, double upper, double alpha, double beta, double gamma, double delta)
  {

      Func1PDF f(alpha, beta, gamma, delta);
      double err_est;
      int err_code;
      const double res = integrate(f, lower, upper, err_est, err_code);
      return res;
  }



  // [[Rcpp::export]]
  NumericVector function_test(double value1, double value2, double alpha, double beta, double gamma) {

  NumericVector res(value1);
  double lower = 1;
  double upper = value2;
  for(R_xlen_t i=1;i<=value1;i++){
    double delta = i;
 res[i-1] = integrate_func(lower, upper, alpha, beta, gamma, delta);

  }
  return res;

  }

来源:https://stackoverflow.com/questions/65288901/fix-memory-leak-in-for-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!