Skip to content

Pass decomposition expr to a lambda instead of storing it to a local#160

Open
sarah-quinones wants to merge 1 commit intojeremy-rifkin:mainfrom
sarah-quinones:fix/temp-lifetime
Open

Pass decomposition expr to a lambda instead of storing it to a local#160
sarah-quinones wants to merge 1 commit intojeremy-rifkin:mainfrom
sarah-quinones:fix/temp-lifetime

Conversation

@sarah-quinones
Copy link
Copy Markdown

@sarah-quinones sarah-quinones commented Apr 29, 2026

i first ran into this bug while using libassert with eigen, which uses expression templates (not very well imo)
writing code like this

void compare(Eigen::MatrixXd a, Eigen::MatrixXd b) {
    LIBASSERT_ASSERT(a * a * a == b);
}

results in undefined behavior, because the a * a produces a temporary that contains references to a, which is then referenced by the expression holding (a * a) * a.
if you store this to a local then the lifetime of that temporary is ended by the time the comparison is actually evaluated, resulting in undefined behavior

my suggested fix is to replace do { auto expr = decomposition; } while (0) with [&](auto expr) { ... } (decomposition), but let me know if you have a different solution

@sarah-quinones sarah-quinones force-pushed the fix/temp-lifetime branch 2 times, most recently from 41cb044 to e45fb13 Compare April 29, 2026 12:29
…so that intermediate lifetimes are not ended too soon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant