Skip to content

Dataset not closing file when object is deleted (or should be) #218

@ChrisBarker-NOAA

Description

@ChrisBarker-NOAA

When a Dataset object is created (write mode), and it then goes out of scope or is deleted, the underlying file does not appear to be closed -- i.e. I get an error if trying to open a file with the same name again.

Example:
import netCDF4
import sys

def test():
    nc = netCDF4.Dataset('junk.nc', mode='w', format='NETCDF4')
    print "refcount after opening:", sys.getrefcount(nc)
    dim = nc.createDimension('fred', 2000)
    print "refcount after creating dimension:", sys.getrefcount(nc)

test()
test()

on the second call to test(), I get an error:
refcount after opening: 2
refcount after creating dimension: 3
Traceback (most recent call last):
File "test_nc_close.py", line 18, in
test()
File "test_nc_close.py", line 11, in test
nc = netCDF4.Dataset('junk.nc', mode='w', format='NETCDF4')
File "netCDF4.pyx", line 1386, in netCDF4.Dataset.init (netCDF4.c:19148)
RuntimeError: Permission denied

I put the refcount report in there to check something -- it seems the refcount of the Dataset object is increasing when the Dimension object is created. This may be the issue. In fact, if you call createDimension() multipel times, each one increases the reference count of the Dataset Object.

I suspect that the Dimension object may be keeping a reference to the Dataset object that it belongs to -- which would create a circular reference and could cause this kind of problem.

Another trick, though -- if you open the Dataset as a NETCDF3_CLASSIC file , there isn't the same problem, and the ref counts behave the same -- so maybe an issue with the hdf library somehow?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions