@EnvironmentObject never gets deallocated

i am presenting a SwiftUI view using UIHostingController which uses an EnvironmentObject that is created when the view is presented:

Code Block swift
let hostingController = UIHostingController(rootView: MyView().environmentObject(ViewModelObject())
present(hostingController, animated: true)


i have noticed that after dismissing the presented view, the EnvironmentObject remains in memory; if the view is presented 3 times, 3 instances of the ViewModelObject remain in memory.

i expected the ViewModelObject to be deallocated once the view is dismissed, but it remains in memory.

What is the best way to ensure that ViewModelObject gets deallocated once the view is no longer onscreen?
Post not yet marked as solved Up vote post of nokey4 Down vote post of nokey4
757 views