I learned that I need to close objects that I open, usually using a with open(): statement that does the closing for you after leaving it. With GDAL/OGR, this does not work and objects must be closed manually. In GDAL, this works by

from osgeo import gdal
ds = gdal.Open(MY_FILE, gdal.GA_ReadOnly)
ds = None

With OGR, I found two ways to do that:

from osgeo import ogr
ds = ogr.Open(MY_FILE)
ds = None

# outputs:
# R6025
# - pure virtual function call

# crashes the debugger


# same

While it seems to work fine with both methods when I run the full script, it crashes my debugger when I run it in debug mode (PyCharm, in my case). In the console, ds = None seems to work, while ds.Destroy() crashes the whole console with the Pure virtual function call output.

Is this specific to PyCharm or am I doing something essentially wrong?


It seems that the problem is related to PyCharm. I have tested it in Anaconda/Spyder and VS Code without a problem.

As I recall properly, the only way of 'closing' a file with GDAL is with

from osgeo import ogr
file = ogr.Open(MY_FILE)
file = None

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.