Segfault when going back to Object Mode, on unrelated data I am writing an Addon.  I have a list of Vectors:

centers = [p.center for p in obj.data.polygons]


In a later section of code, I do the following to a different object:

bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.object.mode_set(mode='OBJECT')


After this return to Object Mode, the earlier list of centers no longer points to anything, and any reference to it causes a segfault.

This is not the only mode change in the code, but the segfault happens only at this one location.
The segfault remains even if I operate on centers_copy = centers.copy().
The segfault goes away if I inelegantly cast the Vectors to lists and back:

centers_list = [list(elt) for elt in centers]
centers = [Vector(elt) for elt in centers_list]


What, precisely, is causing this segfault?

Thanks in advance.
Use a copy

centers = [p.center.copy() for p in obj.data.polygons]


Similarly for obj.location 

&gt;&gt;&gt; C.object.location
Vector((3.3187315464019775, 1.0073195695877075, 7.229274749755859))

&gt;&gt;&gt; v = C.object.location
&gt;&gt;&gt; vc = C.object.location.copy()
&gt;&gt;&gt; C.object.location = (0, 0, 0)
&gt;&gt;&gt; v
Vector((0.0, 0.0, 0.0))

&gt;&gt;&gt; vc
Vector((3.3187315464019775, 1.0073195695877075, 7.229274749755859))


v is a reference to the object location. It is bound to the object. Removing the object and trying to reference v again will lead to a seg fault.

Use v = C.object.location.copy() for an unbound "snapshot" copy.  (casting to list and back was doing this too)

Note centers.copy() was still a copy of a list whose items were references to a polygon and hence the mesh.

As pointed out by @MartinZ you can also set the select values in object mode. The foreach_set(prop, values) method is faster than iterating and setting, eg for polygons

&gt;&gt;&gt; me = C.object.data
&gt;&gt;&gt; me.polygons.foreach_set("select", [False] * len(me.polygons))
&gt;&gt;&gt;