|
| 1 | +# WPF window promoted to process lifetime via a static SystemEvents subscription (region escape) |
| 2 | + |
| 3 | +**Pattern:** a `Window`-scoped dialog strongly subscribes itself to |
| 4 | +`Microsoft.Win32.SystemEvents.DisplaySettingsChanged` — a **static, |
| 5 | +process-lifetime** event source — and keeps no unsubscribe token. The static |
| 6 | +source holds a strong reference to the handler's owner (the dialog) for the whole |
| 7 | +life of the process, so the dialog is *promoted* to process lifetime: it outlives |
| 8 | +its window and lives until the process exits. The bug is the **lifetime mismatch** |
| 9 | +(the dialog expected `Window`, actually `Process`), not any single missing |
| 10 | +`Dispose` call in isolation. This is the SystemEvents leak the .NET docs |
| 11 | +explicitly warn about; distilled from `NickeManarin/ScreenToGif` |
| 12 | +(`GraphicsConfigurationDialog` / `Troubleshoot`). |
| 13 | + |
| 14 | +**What the checker says:** the region-escape theorem (slice #2). With the regions |
| 15 | +declared (`Window < Process`) and the static source tagged `Process`-lived, the |
| 16 | +strong `subscribe self to systemEvents` where the source strictly outlives `self` |
| 17 | +trips the generic **OWN014**: |
| 18 | + |
| 19 | +```text |
| 20 | +$ python -m ownlang check corpus/wpf/systemevents-region-escape/case.own |
| 21 | +case.own:24:23: error: [OWN014] 'systemEvents' (lifetime 'Process') outlives the |
| 22 | + captured object 'GraphicsConfigurationDialog' (lifetime 'Window'); the strong |
| 23 | + subscription promotes 'GraphicsConfigurationDialog' to 'Process' and it leaks |
| 24 | + (no release path) |
| 25 | +``` |
| 26 | + |
| 27 | +**Two views of one bug — token vs region.** The same SystemEvents leak appears in |
| 28 | +the corpus twice, on purpose: |
| 29 | + |
| 30 | +- `corpus/real-world/screentogif-systemevents-leak` models it through the **token |
| 31 | + model** — `event +=` acquires an owned subscription that is never released, so |
| 32 | + the core's **OWN001** fires (a static source is a hard error). |
| 33 | +- **This case** models it through the **region model** — the source is |
| 34 | + process-lived, the subscriber is shorter-lived, and the strong capture promotes |
| 35 | + the subscriber to the longer lifetime, so **OWN014** fires. The *ordering* is |
| 36 | + what makes it a leak: subscribing to a same- or shorter-lived source produces no |
| 37 | + diagnostic (no promotion possible). |
| 38 | + |
| 39 | +The region view is what the C# bridge produces for a static-event `+=`: a |
| 40 | +`capture` OwnIR fact whose `source: "static"` maps to the process-lived region, |
| 41 | +lowered to `subscribe self to <source>` and checked by `ownlang/lifetimes.py` |
| 42 | +(pinned end-to-end by `tests/fixtures/ownir/capture.facts.json`). The region view |
| 43 | +is also more *precise* than the token view — a subscription to an |
| 44 | +equal-or-shorter-lived source is correctly silent, where the flat token model |
| 45 | +would warn. |
| 46 | + |
| 47 | +**Honesty / scope.** `case.own` is a *hand reduction*, not direct C# extractor |
| 48 | +output — `self`/`source` are the function's own scope and its annotated |
| 49 | +parameters; there is no cross-procedural points-to, and weak-event policy as an |
| 50 | +explicit escape hatch is a later slice (see `docs/lifetimes.md`). `before.cs` / |
| 51 | +`after.cs` are representative of the leak and its fix, not a verbatim copy of one |
| 52 | +PR. The fix breaks the static source's hold (here on `Closed`; a disposable token |
| 53 | +works too). |
0 commit comments