Ceres-Solver和G2O中的new是否存在内存泄露

Ceres-Solver和G2O中的new是否存在内存泄露

1. Ceres

在用 Ceres 做优化时,一直都是使用以下方式构建最小二乘问题:

# INPUT_DIM  (1)
# OUTPUT_DIM (3)
// 构建最小二乘问题
ceres::Problem problem;
for (int i = 0; i < N; i++) {
  problem.AddResidualBlock(     // 向问题中添加误差项
    // 使用自动求导,模板参数:误差类型,输出维度,输入维度,维数要与前面struct中一致
    new ceres::AutoDiffCostFunction<CURVE_FITTING_COST, INPUT_DIM, OUTPUT_DIM>(
      new CURVE_FITTING_COST(x_data[i], y_data[i])
    ),
    nullptr,            // 核函数,这里不使用,为空
    abc                 // 待估计参数
  );
}

可以看见上面的代码段 new 了两次,这里 new 出来的是否需要释放呢?如果不手动释放,是否存在内存泄漏的风险?Ceres 文档内有对这一部分做描述:

  1. AutoDiffCostFunction

By default AutoDiffCostFunction will take ownership of the cost functor pointer passed to it, ie. will call delete on the cost functor when the AutoDiffCostFunction itself is deleted. However, this may be undesirable in certain cases, therefore it is also possible to specify DO_NOT_TAKE_OWNERSHIP as a second argument in the constructor, while passing a pointer to a cost functor which does not need to be deleted by the AutoDiffCostFunction.

默认情况下,自动微分代价函数大致长这个样子:

CostFunction* cost_function
    = new AutoDiffCostFunction<MyScalarCostFunctor, 1, 2, 2>(
        new MyScalarCostFunctor(1.0));              ^  ^  ^
                                                    |  |  |
                        Dimension of residual ------+  |  |
                        Dimension of x ----------------+  |
                        Dimension of y -------------------+

默认情况下,AutoDiffCostFunction 将获得传递给它的 cost functor pointer 的所有权。将在 AutoDiffCostFunction 本身被删除时对 cost functor 调用 delete。然而,在某些情况下,这可能是不希望的,因此也可以在构造函数中指定 DO_NOT_TAKE_OWNERSHIP 作为第二个参数,同时传递一个指向 cost functor 的指针,该 cost functor 不需要被 AutoDiffCostFunction 删除。例如:

MyScalarCostFunctor functor(1.0)
CostFunction* cost_function
    = new AutoDiffCostFunction<MyScalarCostFunctor, 1, 2, 2>(
        &functor, DO_NOT_TAKE_OWNERSHIP);
  1. Problem
    Problem 默认接管 cost_function, loss_function, manifold 指针的所有权。这些对象有和 Problem 一样的生命周期。如果用户希望控制这些对象的销毁,可以通过在 Problem::Options 结构体中设置相应的枚举来实现这一点。
    注意,尽管 Problem 接管了对象 cost_functionloss_function 的所有权,但它并不会妨碍用户在另一个残差块中重用它们。类似地,同一个 manifold 对象可以用于多个参数块。析构函数只对每个拥有的对象调用 delete 一次。

总的来说,也就是 cost_functionloss_function 会在 problem 析构时自动释放掉,所以并不会造成内存泄露的问题。

2. G2O

GitHub G2O
与 Ceres 内的内存管理方式异曲同工,G2O 是在将 edgevertex 添加进 optimizer 时,optimizer 接管了指针的归属。optimizer 析构时自动释放 edgevertex

  • 10
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

泠山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值