The osgeo module is not really very "Pythonic" (SWIG generated Python bindings for the GDAL and OGR C/C++ libraries)
Your first solution does not work. Why ?
Look at segfault with Clone() called on GeometryRef of Feature
This is definitely a variable scoping issue. I would call it a bug, but it isn't going to be fixed any time soon. Please use more verbose method calls to ensure intermediate variables do not go out of scope.
The log of the crash confirming the precedent remark is
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x0000000000000090
Therefore this more verbose solution work:
from osgeo import ogr
ds = ogr.Open("test.shp")
layer = ds.GetLayer(0)
# first feature
feat = layer.GetFeature(0)
geom = feat.GetGeometryRef()
When you use
layer.GetFeature(0).Clone() you work with a copy of the feature (different) (OGRFeature * OGRFeature::Clone()) with fewer problems.
That is why Sean Gillies creates Fiona to "simplify" the Python port of the C/C++ ogr library.
layer = fiona.open("test.shp")