# Invalid FileID when trying to close and HDF5 file.

**URL:** <https://forum.hdfgroup.org/t/invalid-fileid-when-trying-to-close-and-hdf5-file/2847>\
**Category:** Hdf-forum archives\
**Created:** [March 16, 2013, 5:37pm UTC](https://forum.hdfgroup.org/t/invalid-fileid-when-trying-to-close-and-hdf5-file/2847 "2013-03-16T17:37:58Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike.jackson](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/mike.jackson/32/1734_2.png) [@mike.jackson](https://forum.hdfgroup.org/u/mike.jackson)\
**Post date:** [March 16, 2013, 5:37pm UTC](https://forum.hdfgroup.org/t/invalid-fileid-when-trying-to-close-and-hdf5-file/2847/1 "2013-03-16T17:37:58Z")

</div>

I am successfully using HDF5 in our project ([DREAM3D.bluequartz.net](http://DREAM3D.bluequartz.net)) but I am getting spurious errors when reading from some of our HDF5 files.

This is the error from the HDF5 library:

HDF5-DIAG: Error detected in HDF5 (1.8.10-patch1) thread 0:  
&nbsp;&nbsp;#000: /Users/mjackson/Workspace/hdf5-1.8.10-patch1/src/H5F.c line 443 in H5Fget\_obj\_count(): not a file id  
&nbsp;&nbsp;&nbsp;&nbsp;major: Invalid arguments to routine  
&nbsp;&nbsp;&nbsp;&nbsp;minor: Bad value  
HDF5-DIAG: Error detected in HDF5 (1.8.10-patch1) thread 0:  
&nbsp;&nbsp;#000: /Users/mjackson/Workspace/hdf5-1.8.10-patch1/src/H5F.c line 2044 in H5Fclose(): invalid file identifier  
&nbsp;&nbsp;&nbsp;&nbsp;major: Invalid arguments to routine  
&nbsp;&nbsp;&nbsp;&nbsp;minor: Inappropriate type

The value of the file id is 16777216 which seems sort of odd as it is always this value no matter how many times I run the program (just an observation). I have a "sentinel" c++ class that keeps an pointer to the file id so when I goes out of scope the HDF5 file will get closed:

class HDF5ScopedFileSentinel  
{  
&nbsp;&nbsp;public:  
&nbsp;&nbsp;&nbsp;&nbsp;HDF5ScopedFileSentinel(hid\_t\* fileId, bool turnOffErrors);  
&nbsp;&nbsp;&nbsp;&nbsp;virtual ~HDF5ScopedFileSentinel();

&nbsp;&nbsp;&nbsp;&nbsp;void setFileId(hid\_t\* fileId);  
&nbsp;&nbsp;&nbsp;&nbsp;hid\_t\* getFileId();

&nbsp;&nbsp;&nbsp;&nbsp;private:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hid\_t\* m\_FileId;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bool m\_TurnOffErrors;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;herr\_t (\*\_oldHDF\_error\_func)(hid\_t, void \*);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;void\* \_oldHDF\_error\_client\_data;  
};

The destructor looks like this:  
HDF5ScopedFileSentinel::~HDF5ScopedFileSentinel()  
{  
&nbsp;&nbsp;if (m\_TurnOffErrors == true)  
&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;H5Eset\_auto(H5E\_DEFAULT, \_oldHDF\_error\_func, \_oldHDF\_error\_client\_data);  
&nbsp;&nbsp;}  
&nbsp;&nbsp;if (\*m\_FileId \> 0) {  
&nbsp;&nbsp;&nbsp;&nbsp;H5Utilities::closeFile(\*m\_FileId);  
&nbsp;&nbsp;&nbsp;&nbsp;\*m\_FileId = -1;  
&nbsp;&nbsp;}  
}

The call to H5Utilities::closeFile is this:  
herr\_t H5Utilities::closeFile(hid\_t &fileId)  
{  
&nbsp;&nbsp;herr\_t err = 1;  
&nbsp;&nbsp;if (fileId \< 0) { // fileId isn't open  
&nbsp;&nbsp;&nbsp;&nbsp;return 1;  
&nbsp;&nbsp;}  
&nbsp;&nbsp;// Get the number of open identifiers of all types  
&nbsp;&nbsp;// except files  
&nbsp;&nbsp;ssize\_t num\_open = H5Fget\_obj\_count(fileId, H5F\_OBJ\_DATASET | H5F\_OBJ\_GROUP |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;H5F\_OBJ\_DATATYPE | H5F\_OBJ\_ATTR);  
&nbsp;&nbsp;if (num\_open \> 0) {  
&nbsp;&nbsp;&nbsp;&nbsp;std::cout \<\< "WARNING: Some IDs weren't closed. Closing them." \<\< std::endl;  
&nbsp;&nbsp;&nbsp;&nbsp;std::vector\<hid\_t\> attr\_ids(num\_open, 0);  
&nbsp;&nbsp;&nbsp;&nbsp;H5Fget\_obj\_ids(fileId, H5F\_OBJ\_DATASET | H5F\_OBJ\_GROUP |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;H5F\_OBJ\_DATATYPE | H5F\_OBJ\_ATTR,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num\_open, &(attr\_ids.front()) );  
&nbsp;&nbsp;&nbsp;&nbsp;for (int i=0; i\<num\_open; i++)  
&nbsp;&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char name[1024];  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;::memset(name, 0, 1024);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//hid\_t obj\_type = H5Iget\_type(attr\_ids[i]);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ssize\_t charsRead = H5Iget\_name( attr\_ids[i], name, 1024);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (charsRead \< 0)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout \<\< "Error Trying to get the name of an hdf object that was not closed. This is probably pretty bad. " \<\< \_\_FILE\_\_ \<\< "(" \<\< \_\_LINE\_\_ \<\< ")" \<\< std::endl;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return -1;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout \<\< "H5 Object left open. Id=" \<\< attr\_ids[i] \<\< " Name='" \<\< name \<\< "'" \<\< std::endl;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;H5Utilities::closeHDF5Object(attr\_ids[i]);  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;}  
&nbsp;&nbsp;err = H5Fclose(fileId);  
&nbsp;&nbsp;if (err \< 0) {  
/\* !!!! I DROP IN HERE !!!!!! \*/  
&nbsp;&nbsp;&nbsp;&nbsp;std::cout \<\< "Error Closing HDF5 File. " \<\< err \<\< std::endl;  
&nbsp;&nbsp;}  
&nbsp;&nbsp;fileId= -1;  
&nbsp;&nbsp;return err;  
}

The code that is the problem is something like this:  
{  
hid\_t fileId = -1;  
fileId = H5Utilities::openFile(m\_InputFile-\>text().toStdString(), true);  
if (fileId \< 0) {  
return;  
}  
// This should automatically close the file when we leave this function  
HDF5ScopedFileSentinel sentinel(&fileId, true);  
// READ A BUNCH OF DATA FROM THE HDF5 File Without any issue  
}

When the sentinel object goes out of scope I get the HDF5 error. If I remove the sentinel object completely and directly call H5Utilities::closeFile(..) then I do NOT get this error. Ideas?

I am crossing Library boundaries with these calls if that matters?

This is all on OS X 10.6.8 with the Stock Xcode compilers (Xcode 3.2.6) and a self built HDF5 1.8.10-patch\_1.

Any ideas on what might the problem be would be great. I can try to pair it down to an more compact example if needed.

> **···**
>
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Mike Jackson Principal Software Engineer  
> BlueQuartz Software Dayton, Ohio  
> [mike.jackson@bluequartz.net](mailto:mike.jackson@bluequartz.net)[www.bluequartz.net](http://www.bluequartz.net)
