google CERES: does ceres free allocated cost functions?

749 views Asked by At

In the non-linear least squares tutorial of the ceres solver http://ceres-solver.org/nnls_tutorial.html the following example is given:

int main(int argc, char** argv) {
  google::InitGoogleLogging(argv[0]);

  // The variable to solve for with its initial value.
  double initial_x = 5.0;
  double x = initial_x;

  // Build the problem.
  Problem problem;

  // Set up the only cost function (also known as residual). This uses
  // auto-differentiation to obtain the derivative (jacobian).
  CostFunction* cost_function =
      new AutoDiffCostFunction<CostFunctor, 1, 1>(new CostFunctor);
  problem.AddResidualBlock(cost_function, NULL, &x);

  // Run the solver!
  Solver::Options options;
  options.linear_solver_type = ceres::DENSE_QR;
  options.minimizer_progress_to_stdout = true;
  Solver::Summary summary;
  Solve(options, &problem, &summary);

  std::cout << summary.BriefReport() << "\n";
  std::cout << "x : " << initial_x
            << " -> " << x << "\n";
  return 0;
}

The heap allocated costfunctions and functors never get deleted. Is that just a simplification of the example or does Ceres take ownership of these objects and delete them?

1

There are 1 answers

0
Solon On

Yes, it takes ownership of functions. You can change this behavior with the following flag.

http://ceres-solver.org/nnls_modeling.html#_CPPv4N5ceres7Problem7Options23cost_function_ownershipE