Disposal of AutoSuspendHelper Instance #4012
Unanswered
JosiahDanger
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello all,
Thank you for your continued effort in developing and maintaining this MVVM framework.
I have a query regarding the ReactiveUI data persistence functionality, which is described in the guidebook on this page. The context of my query is my FOSS app DateToday, which you can find at this GitHub repository. This app is built with Avalonia and ReactiveUI. Avalonia also provides a brief guide to this data persistence functionality here.
So, I have followed the instructions available in both guides to successfully implement data persistence in my app. One facet of this functionality is AutoSuspendHelper class. I have instantiated this class inside my Application (see: App.axaml.cs, line num. 91).
The compiler raises warning CA2000: 'Dispose objects before losing scope'. The guides I referenced earlier do not address how to handle the disposal of objects of this class.
I would like to resolve this warning. I have taken the following steps to attempt to do so:
If I dispose of this object at the end of the 'if' statement block, the app fails to persist its state to disk. This makes sense to me, because my understanding is that this object needs to be present in memory at the time of data persistence in order to facilitate this feature.
If I inject a reference to this object into the constructor of my primary application window, and add it to my disposables, a new bug occurs in which the the application will crash when the user attempts to close it.
Please note that the same bug occurs if I instead call 'desktop.Shutdown()', where 'desktop' is my 'IApplicationLifetime' implementation. It seems that the disposal of the AutoSuspendHelper object in this fashion prevents the closing of the primary application window.
One possible solution that I am not keen on is to subscribe to the 'IApplicationLifetime' 'Exit' event, and dispose of the AutoSuspendHelper instance in an event handler.
This approach does 'work' in that it demonstrably disposes of the AutoSuspendHelper instance, but it has a number of drawbacks in my opinion, including:
Please can you confirm whether it is actually necessary to dispose of this object, and if so, how and when?
Let me know if you would like me to provide additional information.
Kind regards,
Joe
Beta Was this translation helpful? Give feedback.
All reactions