symforce-org / symforce

Fast symbolic computation, code generation, and nonlinear optimization for robotics
https://symforce.org
Apache License 2.0
1.44k stars 147 forks source link

Double free when unused but optimized variable appears appears in cpp solver #380

Closed michael-fennel-skydio closed 9 months ago

michael-fennel-skydio commented 10 months ago

Describe the bug A double free happens within the C++ solver, when a factor is given that contains a variable that is marked as optimized but never appears in the actual problem statement.

To Reproduce

  1. Use any optimization problem formulation in python using the Factor class.
  2. Remove an arbitrary symbolic variable from your problem entirely, but leave its definition.
  3. Add this symbolic variable to the optimized_keys in factor.generate()
  4. Use the generate code in the LM solver.
  5. Watch it crash.

Expected behavior Optimized variable should simply be kept at its initial value,

Environment (please complete the following information):