Hi! I encountered a memory leak issue when trying to use environment object to route the view display of the application. Can you help me to get some idea why the leak happens and how to resolve it?
I use the following AppRootView as the contentView in SceneDelegate.
Following is the class I use for environment variable
When I run the code, neither the MainPageView nor the AgreementView will display, and the memory usage is keep increasing. I added a breakpoint to the AppRoot view, and found that it is running again and again. However, from the call stack I cannot find any clue why this happens.
If I do the following, the application will start without memory leak:
Thank you!
I use the following AppRootView as the contentView in SceneDelegate.
Code Block struct AppRootView: View { @EnvironmentObject var appState: AState var body: some View { appState.updateIsSignedAgreementUpToDate() return VStack{ if false { MainPageView() } else { AgreementView() } } } }
Following is the class I use for environment variable
Code Block class AState : ObservableObject { static let sharedInstance = AState() @Published var isSignedAgreementUpToDate : Bool = false func updateIsSignedAgreementUpToDate() { self.isSignedAgreementUpToDate = false } private init() { } }
When I run the code, neither the MainPageView nor the AgreementView will display, and the memory usage is keep increasing. I added a breakpoint to the AppRoot view, and found that it is running again and again. However, from the call stack I cannot find any clue why this happens.
If I do the following, the application will start without memory leak:
comment out the calling of appState.updateIsSignedAgreementUpToDate() in AppRootView; or
comment out the line of self.isSignedAgreementUpToDate = false in AState class; or
replace the Stack in AppRootView to simply a MainPageView or a AgreementView.
Thank you!