3

Let's suppose we have a class whose state has to be saved and later restored, and we resorted to the Memento pattern for that.

What if the state includes resources that have to be explicitly released (like file handles, OpenGL textures, etc)?

Should I make them part of the saved state? This requires handling memento objects in a special manner: if memento was restored, then it's up to the originator class to handle the resources; if memento is dropped, then it has to clean up itself. Pretty entangled logic.

How would you implement Memento when state has resources to be released?

3
  • Why are you using Memento? If it's to provide undo, then ask yourself if file handles, etc. need to be restored. It's possible to "undo" a file that was opened or saved, but the vast majority of user applications don't do that because it is complicated/difficult (precisely because resource state management is complex). Your question needs more details to get a solid answer, I think. Jul 5 '18 at 15:18
  • What other option do you have? Either it is part of the saved state, or you have some global cache for the resources, and a garbage collector or reference counting. Also, you never explained when the saved state object is "dropped" -- or even which object "owns" the resources when the memento was restored. These determine where and which object should be releasing the resources. Jul 6 '18 at 1:03
  • In fact, my initial design was wrong, I need a new class for resource management. The initial idea was that I have a game scene with loaded objects and their OpenGL resources, when you enter another room list of objects must be replaced but resources have to be preserved so that when the previous state is restored the resources are still valid and can be used further.
    – olegst
    Jul 6 '18 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.