# h5f\_get\_obj\_count\_f Extremely Buggy?

**URL:** <https://forum.hdfgroup.org/t/h5f-get-obj-count-f-extremely-buggy/9678>\
**Category:** HDF5 Library\
**Created:** [April 18, 2022, 4:16pm UTC](https://forum.hdfgroup.org/t/h5f-get-obj-count-f-extremely-buggy/9678 "2022-04-18T16:16:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gkhartman](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/g/dfb087/32.png) [@gkhartman](https://forum.hdfgroup.org/u/gkhartman)\
**Post date:** [April 18, 2022, 4:16pm UTC](https://forum.hdfgroup.org/t/h5f-get-obj-count-f-extremely-buggy/9678/1 "2022-04-18T16:16:26Z")

</div>

I’m trying to determine all open objects using `h5fget_obj_count_f` (HDF5 1.12.1). Within HDF5 source (`H5_ff.f90`) this is accomplished as `CALL h5fget_obj_count_f(INT(H5F_OBJ_ALL_F,HID_T), H5F_OBJ_ALL_F, H5OPEN_NUM_OBJ_LOC, error)`.

The problem is that the returned value `H5OPEN_NUM_OBJ_LOC` is a function of how many times `h5open_f` is called. If you call `h5open_f` once, then call `h5fget_obj_count_f`, you’ll get a different `H5OPEN_NUM_OBJ_LOC` than if you call `h5open_f` two, three, or four times (regardless of whether or not `h5close_f` is called).

Additionally, any other flag besides `H5F_OBJ_ALL_F` in `h5fget_obj_count_f` (e.g. `CALL h5fget_obj_count_f(INT(H5F_OBJ_ALL_F,HID_T), H5F_OBJ_GROUP_F, H5OPEN_NUM_OBJ_LOC, error)` will return nonsense, regardless of how many times `h5open_f` is called.

Is `h5fget_obj_count_f` not the correct way of finding all open objects in an HDF5 under Fortran, or is `h5fget_obj_count_f` just completely broken?

---

<div class="post-metadata">

**Author:** ![brtnfld](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/brtnfld/32/1444_2.png) [@brtnfld](https://forum.hdfgroup.org/u/brtnfld)\
**Post date:** [April 18, 2022, 6:56pm UTC](https://forum.hdfgroup.org/t/h5f-get-obj-count-f-extremely-buggy/9678/2 "2022-04-18T18:56:31Z")

</div>

It looks like a bug where H5close is not resetting the number of open objects, among other issues. I’ll investigate further. Thanks for the report.

[https://jira.hdfgroup.org/browse/HDFFV-11306](https://jira.hdfgroup.org/browse/HDFFV-11306)

---

<div class="post-metadata">

**Author:** ![brtnfld](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/brtnfld/32/1444_2.png) [@brtnfld](https://forum.hdfgroup.org/u/brtnfld)\
**Post date:** [April 18, 2022, 7:27pm UTC](https://forum.hdfgroup.org/t/h5f-get-obj-count-f-extremely-buggy/9678/3 "2022-04-18T19:27:16Z")

</div>

Also, keep in mind that h5open\_f differs from h5open because h5open\_f is used to initialize parameters from the C HDF5 library, and it does this by creating various objects, which get closed later when calling h5close\_f. Therefore, if you call h5open\_f repeatedly, without calling h5close\_f, then those objects created will compound with each call. Currently, there is no iteration in h5close\_f over open objects if you do this, h5close\_f is meant to close only one h5open\_f.  
Just out of curiosity, why are you calling h5open\_f more than once?

The opened objects due to h5open\_f is not meant to be included in the open object count.

---

<div class="post-metadata">

**Author:** ![gkhartman](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/g/dfb087/32.png) [@gkhartman](https://forum.hdfgroup.org/u/gkhartman)\
**Post date:** [April 19, 2022, 5:13am UTC](https://forum.hdfgroup.org/t/h5f-get-obj-count-f-extremely-buggy/9678/4 "2022-04-19T05:13:53Z")

</div>

According to the doc here: [https://support.hdfgroup.org/HDF5/doc/RM/RM\_H5.html#Library-Open](https://support.hdfgroup.org/HDF5/doc/RM/RM_H5.html#Library-Open) (apparently deprecated, but I don’t see other Fortran documentation) `h5open_f` is a required call. I assume, as you note, that at a minimum that’s because it’s setting global constants.

It also says that “there are no damaging side-effects in calling it more than once”, which is apparently incorrect?

My current reason for calling it multiple times is library-based; an application might use HDF5 files and then `use library` that might manipulate its own HDF5 files, and `library` has no control over the caller’s HDF5 state. Absent a library, I could also imagine it being valuable to not need some kind of global `HDF5_OPEN_WAS_CALLED` parameter that every routine has to check before calling `h5open_f` (or alternatively querying some of the global constants that `h5open_f` is setting, but that seems like a fragile and dangerous workaround).

If calling `h5open_f` multiple times is not safe, that seems to be a pretty severe limitation in Fortran applications. Having to look through an entire program and all of its dependencies to verify that `h5open_f` can only ever be called once sounds awful.

---

<div class="post-metadata">

**Author:** ![gheber](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/gheber/32/1919_2.png) [@gheber](https://forum.hdfgroup.org/u/gheber)\
**Post date:** [April 19, 2022, 2:30pm UTC](https://forum.hdfgroup.org/t/h5f-get-obj-count-f-extremely-buggy/9678/5 "2022-04-19T14:30:35Z")

</div>

> [@gkhartman](#):
>
> It also says that “there are no damaging side-effects in calling it more than once”, which is apparently incorrect?

That statement applies to the C-APIs `H5open` and is correct, i.e.,

```auto
H5open();

```

and

```auto
H5open();
...
H5open();

```

behave the same way.

G.

---

<div class="post-metadata">

**Author:** ![brtnfld](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/brtnfld/32/1444_2.png) [@brtnfld](https://forum.hdfgroup.org/u/brtnfld)\
**Post date:** [April 19, 2022, 3:42pm UTC](https://forum.hdfgroup.org/t/h5f-get-obj-count-f-extremely-buggy/9678/6 "2022-04-19T15:42:22Z")

</div>

I made a new PR, [https://github.com/HDFGroup/hdf5/pull/1657](https://github.com/HDFGroup/hdf5/pull/1657), which fixes it so both h5open\_f and h5close\_f can be called multiple times. It also fixes an issue with open objects remaining after h5close\_f was called.  
You can give it a try and see if that works for you.

---

<div class="post-metadata">

**Author:** ![gkhartman](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/g/dfb087/32.png) [@gkhartman](https://forum.hdfgroup.org/u/gkhartman)\
**Post date:** [April 20, 2022, 3:36pm UTC](https://forum.hdfgroup.org/t/h5f-get-obj-count-f-extremely-buggy/9678/7 "2022-04-20T15:36:09Z")

</div>

Looks great, thank you!
