Don't expect OGR to be Pythonic. In fact, there is a large list of gotchas documented to describe these. Essentially, all of the variables are references to objects described in C++, so they often don't make sense with workflows in Python. Modules like copy
only work on Python objects, which is why your example doesn't work as expected. You need to keep references to the features and the geometries alive, not just the geometry, otherwise it will crash.
For example, here is something that will calculate the union of current and previous features, showing the area of all three geometries:
l = d.GetLayer()
fPrevious = None
for fid in range(l.GetFeatureCount()):
fCurrent = l.GetFeature(fid)
if fPrevious:
gCurrent = fCurrent.GetGeometryRef()
gPrevious = fPrevious.GetGeometryRef()
# Do something with the two geometries
gNew = gCurrent.Union(gPrevious)
print("Areas: %d vs %d vs %d" % (gPrevious.GetArea(), gCurrent.GetArea(), gNew.GetArea()))
fPrevious = fCurrent
But if, for example, outside the loop did gNew = gCurrent.Union(gPrevious)
it will crash since the references to the features have changed.