Index: swig/include/python/gdal_python.i
===================================================================
--- swig/include/python/gdal_python.i	(révision 19744)
+++ swig/include/python/gdal_python.i	(copie de travail)
@@ -430,4 +430,33 @@
 }
 %}
 
+// Add a Python reference to prevent premature collection and resulting use
+// of dangling C++ pointer. Intended for methods that return pointers or
+// references to a member variable.
+
+%pythonappend GDALDatasetShadow::GetRasterBand(int nBand) %{
+   if val is not None:
+        val.parentReference = self
+%}
+
+%pythonappend GDALRasterBandShadow::GetOverview(int i) %{
+   if val is not None:
+        val.parentReference = self
+%}
+
+%pythonappend GDALRasterBandShadow::GetMaskBand() %{
+   if val is not None:
+        val.parentReference = self
+%}
+
+%pythonappend GDALRasterBandShadow::GetColorTable() %{
+   if val is not None:
+        val.parentReference = self
+%}
+
+%pythonappend GDALRasterBandShadow::GetRasterColorTable() %{
+   if val is not None:
+        val.parentReference = self
+%}
+
 %import typemaps_python.i
Index: swig/include/python/ogr_python.i
===================================================================
--- swig/include/python/ogr_python.i	(révision 19744)
+++ swig/include/python/ogr_python.i	(copie de travail)
@@ -50,11 +50,13 @@
       "Once called, self has effectively been destroyed.  Do not access. For backwards compatiblity only"
       _ogr.delete_DataSource( self )
       self.thisown = 0
+      self.parentReference = None
 
     def Release(self):
       "Once called, self has effectively been destroyed.  Do not access. For backwards compatiblity only"
       _ogr.delete_DataSource( self )
       self.thisown = 0
+      self.parentReference = None
 
     def Reference(self):
       "For backwards compatibility only."
@@ -198,6 +200,7 @@
       "Once called, self has effectively been destroyed.  Do not access. For backwards compatiblity only"
       _ogr.delete_Feature( self )
       self.thisown = 0
+      self.parentReference = None
 
     def __cmp__(self, other):
         """Compares a feature to another for equality"""
@@ -283,6 +286,7 @@
     self.__swig_destroy__(self) 
     self.__del__()
     self.thisown = 0
+    self.parentReference = None
 
   def __str__(self):
     return self.ExportToWkt()
@@ -326,6 +330,7 @@
     "Once called, self has effectively been destroyed.  Do not access. For backwards compatiblity only"
     _ogr.delete_FeatureDefn( self )
     self.thisown = 0
+    self.parentReference = None
 
 }
 }
@@ -336,7 +341,99 @@
     "Once called, self has effectively been destroyed.  Do not access. For backwards compatiblity only"
     _ogr.delete_FieldDefn( self )
     self.thisown = 0
+    self.parentReference = None
 }
 }
 
+/* Keep the container object alive while the contained */
+/* is alive */
+%pythonappend OGRGeometryShadow::AddGeometryDirectly( OGRGeometryShadow* other_disown ) %{
+   if val == 0 and len(args) > 0 and args[0] is not None:
+        args[0].parentReference = self
+%}
+
+/* Keep the container object alive while the contained */
+/* is alive */
+%pythonappend OGRGeometryShadow::SetGeometryDirectly(OGRGeometryShadow* geom) %{
+   if val == 0 and len(args) > 0 and args[0] is not None:
+        args[0].parentReference = self
+%}
+
+// Add a Python reference to prevent premature collection and resulting use
+// of dangling C++ pointer. Intended for methods that return pointers or
+// references to a member variable.
+
+%pythonappend OGRDataSourceShadow::CreateLayer(const char* name, 
+              OSRSpatialReferenceShadow* srs=NULL,
+              OGRwkbGeometryType geom_type=wkbUnknown,
+              char** options=0) %{
+   if val is not None:
+        val.parentReference = self
+%}
+
+%pythonappend OGRDataSourceShadow::CopyLayer(OGRLayerShadow *src_layer,
+            const char* new_name,
+            char** options=0)
+ %{
+   if val is not None:
+        val.parentReference = self
+%}
+
+%pythonappend OGRDataSourceShadow::GetLayerByIndex( int index=0) %{
+   if val is not None:
+        val.parentReference = self
+%}
+
+%pythonappend OGRDataSourceShadow::GetLayerByName( const char* layer_name) %{
+   if val is not None:
+        val.parentReference = self
+%}
+
+%pythonappend OGRDataSourceShadow::ExecuteSQL(const char* statement,
+                        OGRGeometryShadow* spatialFilter=NULL,
+                        const char* dialect="") %{
+   if val is not None:
+        val.parentReference = self
+%}
+
+%pythonappend OGRLayerShadow::GetSpatialFilter() %{
+   if val is not None:
+        val.parentReference = self
+%}
+
+%pythonappend OGRLayerShadow::GetLayerDefn() %{
+   if val is not None:
+        val.parentReference = self
+%}
+
+%pythonappend OGRFeatureShadow::GetGeometryRef() %{
+   if val is not None:
+        val.parentReference = self
+%}
+
+%pythonappend OGRFeatureShadow::GetDefnRef() %{
+   if val is not None:
+        val.parentReference = self
+%}
+
+%pythonappend OGRFeatureShadow::GetFieldDefnRef(int id) %{
+   if val is not None:
+        val.parentReference = self
+%}
+
+%pythonappend OGRFeatureShadow::GetFieldDefnRef(const char* name) %{
+   if val is not None:
+        val.parentReference = self
+%}
+
+%pythonappend OGRFeatureDefnShadow::GetFieldDefn(int i) %{
+   if val is not None:
+        val.parentReference = self
+%}
+
+%pythonappend OGRGeometryShadow::GetGeometryRef(int geom) %{
+   if val is not None:
+        val.parentReference = self
+%}
+
 %import typemaps_python.i
Index: swig/python/osgeo/gdal.py
===================================================================
--- swig/python/osgeo/gdal.py	(révision 19744)
+++ swig/python/osgeo/gdal.py	(copie de travail)
@@ -575,8 +575,13 @@
 
     def GetRasterBand(self, *args):
         """GetRasterBand(self, int nBand) -> Band"""
-        return _gdal.Dataset_GetRasterBand(self, *args)
+        val = _gdal.Dataset_GetRasterBand(self, *args)
+        if val is not None:
+             val.parentReference = self
 
+
+        return val
+
     def GetProjection(self, *args):
         """GetProjection(self) -> char"""
         return _gdal.Dataset_GetProjection(self, *args)
@@ -843,8 +848,13 @@
 
     def GetOverview(self, *args):
         """GetOverview(self, int i) -> Band"""
-        return _gdal.Band_GetOverview(self, *args)
+        val = _gdal.Band_GetOverview(self, *args)
+        if val is not None:
+             val.parentReference = self
 
+
+        return val
+
     def Checksum(self, *args, **kwargs):
         """Checksum(self, int xoff = 0, int yoff = 0, int xsize = None, int ysize = None) -> int"""
         return _gdal.Band_Checksum(self, *args, **kwargs)
@@ -876,12 +886,22 @@
 
     def GetRasterColorTable(self, *args):
         """GetRasterColorTable(self) -> ColorTable"""
-        return _gdal.Band_GetRasterColorTable(self, *args)
+        val = _gdal.Band_GetRasterColorTable(self, *args)
+        if val is not None:
+             val.parentReference = self
 
+
+        return val
+
     def GetColorTable(self, *args):
         """GetColorTable(self) -> ColorTable"""
-        return _gdal.Band_GetColorTable(self, *args)
+        val = _gdal.Band_GetColorTable(self, *args)
+        if val is not None:
+             val.parentReference = self
 
+
+        return val
+
     def SetRasterColorTable(self, *args):
         """SetRasterColorTable(self, ColorTable arg) -> int"""
         return _gdal.Band_SetRasterColorTable(self, *args)
@@ -900,8 +920,13 @@
 
     def GetMaskBand(self, *args):
         """GetMaskBand(self) -> Band"""
-        return _gdal.Band_GetMaskBand(self, *args)
+        val = _gdal.Band_GetMaskBand(self, *args)
+        if val is not None:
+             val.parentReference = self
 
+
+        return val
+
     def GetMaskFlags(self, *args):
         """GetMaskFlags(self) -> int"""
         return _gdal.Band_GetMaskFlags(self, *args)
Index: swig/python/osgeo/ogr.py
===================================================================
--- swig/python/osgeo/ogr.py	(révision 19744)
+++ swig/python/osgeo/ogr.py	(copie de travail)
@@ -505,8 +505,13 @@
         NULL is returned on failure, or a new OGRLayer handle on success.
         Example: 
         """
-        return _ogr.DataSource_CreateLayer(self, *args, **kwargs)
+        val = _ogr.DataSource_CreateLayer(self, *args, **kwargs)
+        if val is not None:
+             val.parentReference = self
 
+
+        return val
+
     def CopyLayer(self, *args, **kwargs):
         """
         CopyLayer(self, Layer src_layer, char new_name, char options = None) -> Layer
@@ -540,12 +545,22 @@
 
         an handle to the layer, or NULL if an error occurs. 
         """
-        return _ogr.DataSource_CopyLayer(self, *args, **kwargs)
+        val = _ogr.DataSource_CopyLayer(self, *args, **kwargs)
+        if val is not None:
+             val.parentReference = self
 
+
+        return val
+
     def GetLayerByIndex(self, *args):
         """GetLayerByIndex(self, int index = 0) -> Layer"""
-        return _ogr.DataSource_GetLayerByIndex(self, *args)
+        val = _ogr.DataSource_GetLayerByIndex(self, *args)
+        if val is not None:
+             val.parentReference = self
 
+
+        return val
+
     def GetLayerByName(self, *args):
         """
         GetLayerByName(self, char layer_name) -> Layer
@@ -571,8 +586,13 @@
         an handle to the layer, or NULL if the layer is not found or an error
         occurs. 
         """
-        return _ogr.DataSource_GetLayerByName(self, *args)
+        val = _ogr.DataSource_GetLayerByName(self, *args)
+        if val is not None:
+             val.parentReference = self
 
+
+        return val
+
     def TestCapability(self, *args):
         """
         TestCapability(self, char cap) -> bool
@@ -645,8 +665,13 @@
         an handle to a OGRLayer containing the results of the query.
         Deallocate with OGR_DS_ReleaseResultsSet(). 
         """
-        return _ogr.DataSource_ExecuteSQL(self, *args, **kwargs)
+        val = _ogr.DataSource_ExecuteSQL(self, *args, **kwargs)
+        if val is not None:
+             val.parentReference = self
 
+
+        return val
+
     def ReleaseResultSet(self, *args):
         """
         ReleaseResultSet(self, Layer layer)
@@ -678,11 +703,13 @@
       "Once called, self has effectively been destroyed.  Do not access. For backwards compatiblity only"
       _ogr.delete_DataSource( self )
       self.thisown = 0
+      self.parentReference = None
 
     def Release(self):
       "Once called, self has effectively been destroyed.  Do not access. For backwards compatiblity only"
       _ogr.delete_DataSource( self )
       self.thisown = 0
+      self.parentReference = None
 
     def Reference(self):
       "For backwards compatibility only."
@@ -868,8 +895,13 @@
 
         an handle to the spatial filter geometry. 
         """
-        return _ogr.Layer_GetSpatialFilter(self, *args)
+        val = _ogr.Layer_GetSpatialFilter(self, *args)
+        if val is not None:
+             val.parentReference = self
 
+
+        return val
+
     def SetAttributeFilter(self, *args):
         """
         SetAttributeFilter(self, char filter_string) -> OGRErr
@@ -1230,8 +1262,13 @@
 
         an handle to the feature definition. 
         """
-        return _ogr.Layer_GetLayerDefn(self, *args)
+        val = _ogr.Layer_GetLayerDefn(self, *args)
+        if val is not None:
+             val.parentReference = self
 
+
+        return val
+
     def GetFeatureCount(self, *args, **kwargs):
         """
         GetFeatureCount(self, int force = 1) -> int
@@ -1620,8 +1657,13 @@
         an handle to the feature definition object on which feature depends.
 
         """
-        return _ogr.Feature_GetDefnRef(self, *args)
+        val = _ogr.Feature_GetDefnRef(self, *args)
+        if val is not None:
+             val.parentReference = self
 
+
+        return val
+
     def SetGeometry(self, *args):
         """
         SetGeometry(self, Geometry geom) -> OGRErr
@@ -1699,8 +1741,13 @@
         an handle to internal feature geometry. This object should not be
         modified. 
         """
-        return _ogr.Feature_GetGeometryRef(self, *args)
+        val = _ogr.Feature_GetGeometryRef(self, *args)
+        if val is not None:
+             val.parentReference = self
 
+
+        return val
+
     def Clone(self, *args):
         """
         Clone(self) -> Feature
@@ -1795,8 +1842,13 @@
         an handle to the field definition (from the OGRFeatureDefn). This is
         an internal reference, and should not be deleted or modified. 
         """
-        return _ogr.Feature_GetFieldDefnRef(self, *args)
+        val = _ogr.Feature_GetFieldDefnRef(self, *args)
+        if val is not None:
+             val.parentReference = self
 
+
+        return val
+
     def GetFieldAsString(self, *args):
         """
         GetFieldAsString(self, int id) -> char
@@ -2349,6 +2401,7 @@
       "Once called, self has effectively been destroyed.  Do not access. For backwards compatiblity only"
       _ogr.delete_Feature( self )
       self.thisown = 0
+      self.parentReference = None
 
     def __cmp__(self, other):
         """Compares a feature to another for equality"""
@@ -2510,8 +2563,13 @@
         index. This object should not be modified or freed by the application.
 
         """
-        return _ogr.FeatureDefn_GetFieldDefn(self, *args)
+        val = _ogr.FeatureDefn_GetFieldDefn(self, *args)
+        if val is not None:
+             val.parentReference = self
 
+
+        return val
+
     def GetFieldIndex(self, *args):
         """
         GetFieldIndex(self, char name) -> int
@@ -2640,6 +2698,7 @@
       "Once called, self has effectively been destroyed.  Do not access. For backwards compatiblity only"
       _ogr.delete_FeatureDefn( self )
       self.thisown = 0
+      self.parentReference = None
 
 
 FeatureDefn_swigregister = _ogr.FeatureDefn_swigregister
@@ -2890,6 +2949,7 @@
       "Once called, self has effectively been destroyed.  Do not access. For backwards compatiblity only"
       _ogr.delete_FieldDefn( self )
       self.thisown = 0
+      self.parentReference = None
 
 FieldDefn_swigregister = _ogr.FieldDefn_swigregister
 FieldDefn_swigregister(FieldDefn)
@@ -3038,8 +3098,13 @@
 
     def AddGeometryDirectly(self, *args):
         """AddGeometryDirectly(self, Geometry other_disown) -> OGRErr"""
-        return _ogr.Geometry_AddGeometryDirectly(self, *args)
+        val = _ogr.Geometry_AddGeometryDirectly(self, *args)
+        if val == 0 and len(args) > 0 and args[0] is not None:
+             args[0].parentReference = self
 
+
+        return val
+
     def AddGeometry(self, *args):
         """AddGeometry(self, Geometry other) -> OGRErr"""
         return _ogr.Geometry_AddGeometry(self, *args)
@@ -3157,8 +3222,13 @@
 
     def GetGeometryRef(self, *args):
         """GetGeometryRef(self, int geom) -> Geometry"""
-        return _ogr.Geometry_GetGeometryRef(self, *args)
+        val = _ogr.Geometry_GetGeometryRef(self, *args)
+        if val is not None:
+             val.parentReference = self
 
+
+        return val
+
     def GetBoundary(self, *args):
         """
         GetBoundary(self) -> Geometry
@@ -4022,6 +4092,7 @@
       self.__swig_destroy__(self) 
       self.__del__()
       self.thisown = 0
+      self.parentReference = None
 
     def __str__(self):
       return self.ExportToWkt()
