Repository navigation
Conversation
A `@pybind_lambda` adapter declares the signature the binding should expose, but the emitted lambda deduced its return type from the C++ callable. When that callable returns a non-owning view by value, such as an Eigen block, pybind11 casts it as a view with no parent, so the array dangles once a temporary owner is destroyed. In GTSAM, `GaussianConditional.d()`, `JacobianFactor.getA()`/`getb()` and `HessianFactor.linearTerm()` are declared as owning `Vector`/`Matrix` adapters, yet return such arrays. Adapters that return by value now state the declared return type explicitly, so the result is converted to it. Adapters that return `const T&` keep `reference_internal`; void and reference returns and unannotated lambdas are unchanged. The fixture gains an adapter whose C++ callable returns a view, and the existing test compiles it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A
@pybind_lambdaadapter declares the signature its binding should expose, but the emitted lambda deduced its return type from the C++ callable. When that callable returns a non-owning view by value, such as an Eigen block, pybind11 casts it as a view without a parent. The NumPy array then dangles once a temporary owner is destroyed.GTSAM relies on these adapters to do exactly that conversion. Its
linear.ideclaresJacobianFactor.getA()/getb()as@pybind_lambda gtsam::Matrix/gtsam::Vector, with the comment "The C++ methods return Eigen views, and the wrapper materializes them at the language boundary." The wrapper never did.getA(),getb(),GaussianConditional.d()andHessianFactor.linearTerm()return arrays that don't own their data, and on a temporary they read freed memory:gtsam.JacobianFactor(0, A, b, model).getA()returns garbage. This reproduces with gtsam-develop 4.3.2.dev202610300138, 4.3.0 and 4.2.2.No interface declaration can express the intended binding today:
@pybind_lambdais the documented way to declare a different signature, but it ignored the declared return type.Change.
@pybind_lambdaadapters now state the declared return type, for example[](T* self) -> gtsam::Vector{return self->d();}. This applies to methods, static methods and global functions.const T&keepreference_internal.Tests. The adapter fixture gains a callable whose C++ returns a
View<T>. The expected output and assertions are updated, and the existing test compiles the generated code.Validation.
uv sync --locked --all-extras --dev,cmake .,uv run pytest tests) on a fresh clone ofmasterwith this patch, on Python 3.10, 3.11 and 3.13, plus 3.12 with plainpytest. 137 tests passed on each..agents/skills/annotate-pybind-adapters. It adds no symbol lists or GTSAM-specific heuristics, MATLAB output is unchanged, and Python names, defaults, policies, docstrings and overloads are preserved.Effect on GTSAM.
getA,getb,d,linearTerm) now return owned arrays. The other 27 gain the same type that was deduced before (Key,char,Pose3,Vector6,std::shared_ptr<...>, ...).Map,Ref,Block, also inside pair, tuple, vector or optional) returned by value. Acrossgtsam.i, its 22 submodules andgtsam_unstable.i, exactly six bindings hand out views without keeping their owner alive, all inlinear: the four above plusGaussianConditional.R()/S(). With this change and the follow-up GTSAM change, none remain. The optional CUDA and MOSEK interfaces were not scanned.