Swift 5.10 "release" builds garbage collects local variables too aggressively #73442
Labels
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
triage needed
This issue needs more specific labels
Description
When building a program with swift 5.10 using the "release" configuration (
-c release
), local variables within a function may be freed before the function returns.Perhaps this is as designed, but is different from when building using the "debug" configuration, or previous versions of Swift ("debug" or "release").
I've provided this simple application to demonstrate my observations. Details below.
Reproduction
Expected behavior
The
tick
publisher is expected to publish a date every second, until the program is terminated:The expected behavior is observed with "debug" builds (
-c debug
) in Swift 5.10. It is also observed in Swift 5.9 (both "debug" and "release" builds).When using the Swift 5.10 "release" configuration however, note that the publisher is immediately cancelled, before emitting anything. This is presumably because
subscriptions
has been garbage collected:Environment
swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-macosx14.0
Additional information
Introducing a reference to
subscriptions
at the end of the function block by uncommenting the following:works around the problem.
Or perhaps more cleanly having a
defer
block that does something withsubscriptions
, for instance:The text was updated successfully, but these errors were encountered: