Skip to content

Fix #594: Memory issue with Map keys and Vector.push_back()#650

Open
leftibot wants to merge 1 commit intoChaiScript:developfrom
leftibot:fix/issue-594-memory-issue-with-map-keys-and-vector-pu
Open

Fix #594: Memory issue with Map keys and Vector.push_back()#650
leftibot wants to merge 1 commit intoChaiScript:developfrom
leftibot:fix/issue-594-memory-issue-with-map-keys-and-vector-pu

Conversation

@leftibot
Copy link
Copy Markdown
Contributor

Automated fix by @leftibot.

What changed

Fix #594: Map keys become dangling references when pushed into Vector
The Handle_Return_Ref specialization for const references was wrapping
return values in std::cref() while marking them as return values
(is_return_value=true). This caused Vector.push_back() to store the
reference directly without cloning, since it assumes return values are
freshly created temporaries. When the source object (e.g., a map) went
out of scope, the vector contained dangling references to freed memory.
The fix sets is_return_value=false for const reference returns, which
correctly triggers push_back to clone the value instead of storing a
bare reference. This is consistent with the non-const reference handler
(Handle_Return<Ret &>) which also does not set the return value flag.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Files

 include/chaiscript/dispatchkit/handle_return.hpp |  2 +-
 unittests/map_keys_vector_push_back.chai         | 29 ++++++++++++++++++++++++
 2 files changed, 30 insertions(+), 1 deletion(-)

Closes #594

Triggered by @lefticus.

…nto Vector

The Handle_Return_Ref specialization for const references was wrapping
return values in std::cref() while marking them as return values
(is_return_value=true). This caused Vector.push_back() to store the
reference directly without cloning, since it assumes return values are
freshly created temporaries. When the source object (e.g., a map) went
out of scope, the vector contained dangling references to freed memory.

The fix sets is_return_value=false for const reference returns, which
correctly triggers push_back to clone the value instead of storing a
bare reference. This is consistent with the non-const reference handler
(Handle_Return<Ret &>) which also does not set the return value flag.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
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.

Memory issue with Map keys and Vector.push_back()

1 participant