SwiftUI's toolbar APIs on macOS have several issues with memory leaks

Originator:matt.curtis.biz
Number:rdar://FB9453009 Date Originated:
Status:Closed Resolved:
Product:SwiftUI Product Version:macOS 11
Classification:Bug Reproducible:
 
Please describe the issue:
On macOS, SwiftUI's APIs for adding toolbar items to a window have (at least) two bugs around view recreation that lead to memory leaks and increased CPU pressure over time.

Issue #1: ToolbarItem always creates an additional, "ghost" copy of the view it contains
Issue #2: ToolbarItem creates an additional ghost copy of the view it contains every single time state changes

(I've also written about and detailed this issue here: https://github.com/matt-curtis/mac-swiftui-toolbar-memory-leak-demo)
Please list the steps you took to reproduce the issue:
1. Open the attached sample project.
2. Run the attached sample project.
What did you expect to happen?
SwiftUI should not create unneeded views that waste memory and CPU.
What actually happened?
Views are re-created unnecessarily, CPU pressure increases, and memory leaks.

Comments

This bug appears to be fixed in Monterey (macOS 12)

By matt.curtis.biz at Nov. 14, 2021, 1:08 a.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!