how to save object data while changing mode (Object mode, Edit mode)?
The mesh data stored for an object is different from the bmesh data used while in edit mode, while the object data is static and used for drawing and rendering the edit mesh is optimized for adding and deleting items. When you enter edit mode the mesh data is copied to a bmesh structure which is copied back and destroyed when you go back to object mode. This is why you get the error as the bmesh data that you are using has been destroyed and no longer exists.

I expect you should adjust your code to not toggle in and out of object mode, you should be able to do what you want while the object stays in edit mode or perform the steps after you have finished in edit mode.

One option is to stay in object mode, you don't have to go into edit mode to alter the mesh, unless the user is interactively editing it. You can create the bmesh, edit the mesh and copy it back to the object, using bm.to_mesh() multiple times.

import bpy
import bmesh

me = bpy.context.object.data

bm = bmesh.new()
bm.from_mesh(me)

for v in bm.verts:
    v.co.x += 1.0
bm.to_mesh(me)

for v in bm.verts:
    v.co.y += 1.0
bm.to_mesh(me)

for v in bm.verts:
    v.co.z += 1.0
bm.to_mesh(me)

bm.free()
bpy.context.scene.frame_set(bpy.context.scene.frame_current)