Skip to content

Lifetime management: Make tracked peer storage the explicit codegen default + add storage-lint scaffolding - #11876

Open
Protik Biswas (protikbiswas100) wants to merge 1 commit into
mainfrom
user/protikbiswas/tracked-peer-storage-default
Open

Protik Biswas (protikbiswas100) wants to merge 1 commit into
mainfrom
user/protikbiswas/tracked-peer-storage-default

Conversation

@protikbiswas100

@protikbiswas100 Protik Biswas (protikbiswas100) commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

What

Makes tracked (GC-visible) peer storage the explicit codegen default for XAML object fields, and adds storage-lint scaffolding to enforce it.

Fixes

https://task.ms/64051051

Why

XAML objects exist as a native/managed pair whose lifetimes must stay in sync. When a peer reference is stored in a way the GC walk cannot see (a plain pointer/ComPtr field), it becomes invisible to the peer-lifetime tracker and can leak or be released prematurely. Defaulting field storage to the tracked form — and adding a lint that flags untracked peer fields — closes that gap by construction.

Changes

  • Emit tracked peer storage as the explicit default from the XamlGen codegen (field getter/setter templates, type/property metadata, TrackerTargetTraits).
  • Add clang-tidy PeerComPtrFieldCheck storage-lint scaffolding (tools/clang-tidy/XamlChecks/) to catch untracked peer fields.

Net: 23 files changed, 2236 insertions (additive only, no deletions).

Notes

  • Cherry-picked cleanly onto main; contains this single commit.

…lint scaffolding

Storage correctness by construction: reference (peer) fields default to
GC-visible tracked storage, opting out is explicit and greppable, and raw
ComPtr<peer> fields become lintable.

Codegen (authoring side):
- Add TypeDefinition/TypeReference.IsTrackerTarget as the single authority for
  "reference types default to TrackerPtr storage" and make
  PropertyDefinition.FrameworkFieldTypeName select storage through an explicit
  ladder. Behavior-preserving: regenerating all codegen produces byte-identical
  output (verified: 1801 generated files unchanged, 0 diffs).
- Add a [PropertyFlags(WeakRef=true)] opt-out that emits ctl::WeakRefPtr, the
  explicit alternative to the tracked default. UseComPtr (tied to
  AllowCrossThreadAccess) is preserved and checked ahead of the default.
- Emit TrackerTargetTraits.g.h: one is_tracker_target<T> specialization per
  peer type, from the same predicate, so the storage default and the lint share
  one source of truth. Primary template in TrackerTargetTraits.h.

Lint (enforcement side), under tools/clang-tidy:
- New clang-tidy check xaml-peer-comptr-field (PeerComPtrFieldCheck) that errors
  on a raw ComPtr<T> field when is_tracker_target<T> is true, allowlisting
  TrackerPtr, ctl::WeakRefPtr and NonTracked<T>.
- Module registration, CMake, a .clang-tidy.example (warning-as-error, scoped to
  peer dirs) and README. The module builds against an LLVM/clang-tidy tree and
  is not part of the product build; rollout is warn-only then error per the
  design phasing.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 1677a4a7-252f-4dae-82bb-c43c6e88340c
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@microsoft-github-policy-service microsoft-github-policy-service Bot added the needs-triage Issue needs to be triaged by the area owners label Sep 14, 2026
@protikbiswas100

Copy link
Copy Markdown
Contributor Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
1 pipeline(s) were filtered out due to trigger conditions.

@protikbiswas100 Protik Biswas (protikbiswas100) changed the title Make tracked peer storage the explicit codegen default + add storage-lint scaffolding Lifetime management: Make tracked peer storage the explicit codegen default + add storage-lint scaffolding Sep 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-triage Issue needs to be triaged by the area owners

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant