Skip to content

fixes #773 - release the StructMeta reference held by every Struct - #774

Closed
jieya907 wants to merge 1 commit into
Point72:mainfrom
jieya907:fix-773-struct-meta-refcount
Closed

jieya907 wants to merge 1 commit into
Point72:mainfrom
jieya907:fix-773-struct-meta-refcount

Conversation

@jieya907

@jieya907 jieya907 commented Oct 6, 2026

Copy link
Copy Markdown

Fixes #773

Struct::Struct placement-constructs HiddenData, whose std::shared_ptr<const StructMeta> takes a reference to the struct's meta, but ~Struct never destroyed it. Every struct 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 frees the meta, and StructMeta::initialize then reads freed memory. The repro and backtrace are in #773.

This change destroys HiddenData at the end of ~Struct, after meta() -> destroy( this ), so the fields are still destroyed while the meta is alive.

Tests

  • New cpp/tests/engine/test_struct.cpp checks that StructMeta::use_count() returns to its baseline after 1000 create()/copy()/deepcopy() cycles for plain, nested and derived struct types, and that live structs still hold a reference. All four cases fail on main (for example, the use count is 3001 instead of 1 after the derived-type loop) and pass with this change.
  • No Python test is added. Python-defined struct types are kept alive by the existing PyStructMeta ↔ DialectStructMeta cycle 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.
  • C++ (make test-cpp): with this change all 12 test binaries pass, in both a Release build and a Debug build with CSP_ENABLE_ASAN=1 and LeakSanitizer. On main, only the new test_struct fails.
  • Python (pytest csp/tests): identical results with and without this change: 1622 passed, 25 skipped, 2 xfailed.
  • make lint passes.

…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>
@robambalu robambalu added type: bug Concrete, reproducible bugs good first issue Good issue for first-time contributors labels Oct 6, 2026
YQteam-dyq

This comment was marked as spam.

@timkpaine timkpaine closed this Nov 29, 2026
@timkpaine timkpaine mentioned this pull request Nov 29, 2026
3 tasks
@robambalu

Copy link
Copy Markdown
Collaborator

This is actually a good fix I havent had a chance to fully eval but looks correct

@robambalu robambalu reopened this Nov 30, 2026
@robambalu

Copy link
Copy Markdown
Collaborator

This is actually a good fix I havent had a chance to fully eval but looks correct

I see it was re-opened here: #806
not sure why we're closing and re-opening PRs

@robambalu robambalu closed this Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

good first issue Good issue for first-time contributors type: bug Concrete, reproducible bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

csp::Struct leaks one StructMeta reference per construction

4 participants