Repository navigation
Conversation
…ruct Struct::Struct placement-constructs HiddenData, whose std::shared_ptr<const StructMeta> takes a reference to the struct's meta, but ~Struct never destroyed HiddenData. Every struct ever created therefore leaked one StructMeta reference. libstdc++ keeps the use count in a 32-bit int, so after about 2^32 constructions of one struct type the count wraps: a shared_from_this() temporary in StructMeta::createRaw then releases what looks like the last reference, the meta is freed, and StructMeta::initialize reads freed memory and crashes. Destroy HiddenData at the end of ~Struct, after the fields have been destroyed while the meta is still alive. Add cpp/tests/engine/test_struct.cpp, which checks that a StructMeta's use_count() is unchanged after create()/copy()/deepcopy() cycles for plain, nested and derived struct types, and that live structs still hold their reference. Signed-off-by: Susan Wen <9298414+jieya907@users.noreply.github.com>
jieya907
requested review from
AdamGlustein,
alexddobkin,
arhamchopra,
ptomecek,
robambalu,
svatasoiu and
timkpaine
as code owners
October 6, 2026 21:09
Collaborator
|
This is actually a good fix I havent had a chance to fully eval but looks correct |
Collaborator
I see it was re-opened here: #806 |
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.
Fixes #773
Struct::Structplacement-constructsHiddenData, whosestd::shared_ptr<const StructMeta>takes a reference to the struct's meta, but~Structnever destroyed it. Every struct therefore leaked oneStructMetareference. libstdc++ keeps the use count in a 32-bitint, so after about 2^32 constructions of one struct type the count wraps: ashared_from_this()temporary inStructMeta::createRawfrees the meta, andStructMeta::initializethen reads freed memory. The repro and backtrace are in #773.This change destroys
HiddenDataat the end of~Struct, aftermeta() -> destroy( this ), so the fields are still destroyed while the meta is alive.Tests
cpp/tests/engine/test_struct.cppchecks thatStructMeta::use_count()returns to its baseline after 1000create()/copy()/deepcopy()cycles for plain, nested and derived struct types, and that live structs still hold a reference. All four cases fail onmain(for example, the use count is 3001 instead of 1 after the derived-type loop) and pass with this change.PyStructMeta↔DialectStructMetacycle either way, so the leak shows up only in the C++ use count, which Python can't read without ctypes layout tricks or new API.make test-cpp): with this change all 12 test binaries pass, in both a Release build and a Debug build withCSP_ENABLE_ASAN=1and LeakSanitizer. Onmain, only the newtest_structfails.pytest csp/tests): identical results with and without this change: 1622 passed, 25 skipped, 2 xfailed.make lintpasses.