.accessibilityElement(children: .contain) produces memory leaks in UITests

We were encourage by Frameworks Engineer reply and Apple's Official Docs and started to use this approach in our App. But next we detected that App has leaked objects when we do Memory Graph Check in UITests.

Removing of .accessibilityElement(children: .contain) solves the issue but we still want to use container identifiers in our UITests.

Here is a sample project to illustrated and reproduce the issue: https://github.com/yuri-qualtie/MemoryLeaks. All the details and steps are described in Readme

We need help with fix for .accessibilityElement(children: .contain) but any workaround is welcomed

Same issue happens by just using Accessibility on macOS, like accessibility inspector or Voice Over, not only in UI Tests.

To reproduce:

open your sample appopen accessibility inspectorfocus on the current app view in inspectornavigate awaysee object graph showing previous view model is not released

Hello,

Thanks for reporting your findings. Could you please share what OS and version you are experiencing memory leaks on?

Thanks,

Nathan | SwiftUI Accessibility

Thank you Nathan,

We see this on macOS Big Sur, specifically 11.5.2.

FB9188594: Object graph shows strong reference only when we use Accessibility features (either by UI tests or even by Accessibility Inspector). In our AppStore app this creates memory footprint of 2.5 Gb instead of usual 45 Mb within 30 min of using the app. Basically every time user refreshes the view our memory footprint grows by megabytes.

Appreciate any help!

Nathan, if it helps I opened
Feedback Assistant: FB9584439
TSI Case ID: 779392495

Hello,

Thanks for your excellent feedback report, I was easily able to test your sample project.

The problem you reported sounded familiar as much effort was put in this last year to improve performance of SwiftUI accessibility. You will be pleased to find that in macOS Monterey (12.0) the memory leaks have been fixed! Using the latest macOS 12 beta, I could not reproduce the memory leak.

As I'm sure you would like your app to target macOS 11, one possible workaround would be to adjust where you add the accessibilityElement(children: .contain) modifier. Rather than using it as a modifier to your PickerWithBindingView, the modifier could be used within the var body: some View of PickerWithBindingView.

Best,

Nathan | SwiftUI Accessibility

Hello Nathan,

Thank you for investigation and response. I'm glad that my report helped to detect the issue. We as a first adopters of SwiftUI are really inspired to make it better and help to overcome young diseases.

Good news that macOS Monterey (12.0) doesn't have this issue. We will update our development and CI instances to Monterey when it is officially released. Unfortunately, we don't want to increase minimum requirements for our customers so we are definitely interested in a workaround. I tried that you suggested for var body: some View of PickerWithBindingView but still have memory leaks.

My steps:

  1. Deleted all the .containerAccessibility from MainView.swift
  2. Added in PickerWithBindingView.swift:
        VStack {
            ..... previous body
        }
        .accessibilityElement(children: .contain)
        .accessibilityIdentifier("picker-with-binding-view")

I pushed it to separate branch: https://github.com/yuri-qualtie/MemoryLeaks/tree/try-workaround

Could you please try? Maybe I misunderstood your suggestion for workaround.

Thanks,
Yuri-

.accessibilityElement(children: .contain) produces memory leaks in UITests
 
 
Q