[gdal-dev] Python IOError during file object destructor

Even Rouault even.rouault at spatialys.com
Wed Aug 21 04:03:15 PDT 2019


Craig,

gdal.GDALDestroyDriverManager() exists in the SWIG binding but I would 
definitely not recommend using it. At best, it will be a no-op. At worst, this 
will result in double free of datasets.

The only situations I can think of where you would have still-alive GDAL 
datasets at Python process close are:
- global variables storing datasets
- main program exiting with still open datasets.

And in both cases, calling explicitly gdal.GDALDestroyDriverManager() will 
result in a crash

Demo:

given test.py being
```
from osgeo import gdal

# ds will be released when f() returns
def f():
    ds = gdal.Open('../autotest/gcore/data/byte.tif')

f()
print('foo')
ds = gdal.Open('../autotest/gcore/data/byte.tif')
gdal.GDALDestroyDriverManager()
print('bar')
```

```
$ CPL_DEBUG=ON python test.py
GDAL: GDALOpen(../autotest/gcore/data/byte.tif, this=0x18eaa00) succeeds as 
GTiff.
GDAL: GDALClose(../autotest/gcore/data/byte.tif, this=0x18eaa00)
foo
GDAL: GDALOpen(../autotest/gcore/data/byte.tif, this=0x18eaa00) succeeds as 
GTiff.
GDAL: Force close of ../autotest/gcore/data/byte.tif (0x18eaa00) in 
GDALDriverManager cleanup.
GDAL: GDALClose(../autotest/gcore/data/byte.tif, this=0x18eaa00)
bar
GDAL: In ~GDALMajorObject on invalid object
*** Error in `python': corrupted size vs. prev_size: 0x00000000018eae30 ***
======= Backtrace: =========
[... crash ...]
```

Even

-- 
Spatialys - Geospatial professional services
http://www.spatialys.com


More information about the gdal-dev mailing list