Undo breaks my addon

Hello all! First-timer here :wink:

I am writing addon, that adds keyframes to a group of objects at a time.

In example .blend file I created two objects: Group_obj1 and Group_obj2.

If you select both of them and go to View3D > Sidebar > View Tab > Group Poses you can add keyframes to both of them when you hit Add Keypose button, both object will get keyframes by location at frame 1.

What I do next to break addon:

  1. Go to frame 100. Hit button Add Keypose
  2. Go to frame 150 and change Y location of one of the objects. Hit Add Keypose
  3. Press Ctrl+Z so keyframes in frame 150 are not set.
  4. Hit Add Keypose again and I get an error:
Traceback (most recent call last):

  File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\scripts\addons\out_exmpl\__init__.py", line 98, in execute
    GroupObjectsData.add_keypose(current_frame)
  File "C:\Program Files\Blender Foundation\Blender 2.83\2.83\scripts\addons\out_exmpl\__init__.py", line 27, in add_keypose
    x, y, z = obj.location
ReferenceError: StructRNA of type Object has been removed

How can I handle this Undo, so nothing breaks?

I want to keep all backend associated with inserting keyframes and other animation stuff in my own class GroupObjects, which is not inherrited from bpy.

My __init__.py: https://gist.github.com/spawly/3ceee6897519e74c3d3cddd3f96ef073

My .blend file with all objects added: https://drive.google.com/file/d/1KlRZgFnQbrlKr349BMgAlxDyQM0--d8x/view?usp=sharing

Any help is very-very appreciated. My first time writing aby addon for blender :slightly_smiling_face: