Is it possible to add a global undo button?

I tested this implementation that derives from the original work you linked and did not observe any crash in the scenario you described. May be you could give it a try.