Files
sx/examples/1325-ffi-objc-arc-02-strong-property.sx
agra 4e942b5373 test: migrate examples to XXXX-category-name layout + split expected streams
Rename all example tests/companions to the XXXX-category-test-name scheme
(per-category 100-blocks: basic 0010, types 0100, ... errors 1000,
diagnostics 1100, ffi 1200, ffi-objc 1300, ffi-jni 1400, vectors 1500,
platform 1600). Companions and dir/C fixtures move in lockstep with their
parent test; #import/#source/#include paths rewritten to match.

Expected output now lives in examples/expected/ (a sibling dir of the
tests) split into three streams per the new convention:
  <name>.exit / <name>.stdout / <name>.stderr  (+ optional <name>.ir)

run_examples.sh rewritten: scans examples/ and issues/ for an
expected/<name>.exit marker, captures stdout and stderr separately (no
more 2>&1), compares each stream + exit + optional IR snapshot.

Behavior validated unchanged: every renamed test reproduces its prior
merged output + exit (diffs limited to file paths/basenames embedded in
diagnostics + traces, which correctly reflect the new names). Suite:
292 passed, 0 failed. 50-smoke.sx split + issue relocation + docs follow
in subsequent commits.
2026-06-01 19:05:15 +03:00

79 lines
2.8 KiB
Plaintext

// ffi-objc-arc-02 — #property(strong) on sx-defined class.
//
// Strong contract:
// - setter retains the new value, releases the old.
// - -dealloc releases each strong property ivar before freeing state.
//
// Observation: a child instance assigned to a parent's strong property
// stays alive after we drop our own reference. Without the strong
// setter, the child's refcount drops to 0 immediately when we release,
// and the parent ends up with a dangling pointer.
//
// We observe via TrackingAllocator. The KEY check is the dealloc count
// AT THE MIDPOINT — between dropping our child reference and releasing
// the parent. With strong+dealloc-cleanup: child stays alive across
// midpoint (no extra dealloc). Without: child is dead at midpoint.
#import "modules/std.sx";
#import "modules/allocators.sx";
#import "modules/std/objc.sx";
#import "modules/compiler.sx";
SxStrongChild :: #objc_class("SxStrongChild") {
#extends NSObject;
tag: s32;
alloc :: () -> *SxStrongChild;
}
SxStrongParent :: #objc_class("SxStrongParent") {
#extends NSObject;
child: *SxStrongChild #property(strong);
alloc :: () -> *SxStrongParent;
}
main :: () -> s32 {
inline if OS == .macos {
gpa := GPA.init();
tracker := TrackingAllocator.init(xx gpa);
push Context.{ allocator = xx tracker, data = null } {
d0 := tracker.dealloc_count;
a0 := tracker.alloc_count;
parent := SxStrongParent.alloc();
child := SxStrongChild.alloc();
parent.child = child; // dispatches setChild: via M2.2 property machinery
child.release();
// Midpoint: with strong setter, child is retained by parent;
// tracker.dealloc_count should NOT have advanced beyond d0.
// Without strong setter, child auto-dealloc'd on release.
d_mid := tracker.dealloc_count;
if d_mid != d0 {
print("FAIL: child dealloc'd at midpoint (strong setter not retaining); delta={}\n",
d_mid - d0);
return 1;
}
parent.release();
// After parent.release: parent.dealloc fires, releases the
// strong child ivar, child deallocs, state freed.
d_end := tracker.dealloc_count;
// Net: 2 allocs (parent + child state), 2 deallocs (both freed).
// Balance check: dealloc delta == alloc delta.
if d_end - d0 != tracker.alloc_count - a0 {
print("FAIL: unbalanced; alloc={} dealloc={}\n",
tracker.alloc_count - a0, d_end - d0);
return 1;
}
}
print("strong property: ok\n");
}
inline if OS != .macos {
print("skipped (not macos)\n");
}
0;
}