(This reply was certainly meant to go to the forum)
------- Forwarded message -------
From: "Ger van Diepen" <diepen@astron.nl>
To: "Werner Benger" <werner@cct.lsu.edu>
Cc:
Subject: Re: [hdf-forum] Again on H5close
Date: Thu, 14 May 2009 07:24:52 -0500
It looks as if we are talking C++ here.
IMHO it is much better to make simple classes like HDFGroup, HDFFile, etc.
keeping the hid internally. Their destructors should call the appropriate
close function.
In this way you do not have to worry about closing HDF objects. It's done
for you when the object goes out of scope. What's more important, in case
of exceptions they are automatically closed for you, so no hid leaks or so.
// <synopsis>
// This class wraps an HDF5 file hid (hdf5 id). It offers two benefits:
// <ul>
// <li> The most important is resource management. In case of an
exception,
// the hid will automatically be closed by the destructor.
// <li> A hid is a kind of pointer and should not be copied. This class
// forbid making a copy, but make it possible to use it in a
// shared pointer context.
// </ul>
// </synopsis>
class HDFFile
{
public:
// Default constructor sets hid to invalid.
HDFFile()
: itsHid(-1) {}
// Construct from given hid.
HDFFile (hid_t hid)
: itsHid(hid) {}
// The destructor closes the hid.
~HDF5HidAttribute()
{ close(); }
// Close the hid if valid.
void close();
// Put hid in it. If it already contains a hid, it will be closed.
void operator= (hid_t hid)
{ close(); itsHid = hid; }
// Get the hid.
hid_t hid() const
{ return itsHid; }
// Convert automatically to hid_t.
operator hid_t() const
{ return itsHid; }
private:
// Copy constructor cannot be used.
HDFFile (const HDFFile& that);
// Assignment cannot be used.
HDFFile& operator= (const HDFFile& that);
hid_t itsHid;
};
The same for HDFGroup, HDFDataSet, etc.
I would even be better to have a base class HDFObject keeping the hid.
It could be used like:
{
HDFFile hfile(H5Fopen(...));
HDFGroup hgroup(H5Gopen(hfile, ....))
.. other code ..
} // out-of-scope thus group and file get closed
I've done something like this and it works very nicely.
Cheers,
Ger
"Werner Benger" <werner@cct.lsu.edu> 05/14/09 1:12 PM >>>
You (allmost certainly) did not close all of the opened and created HDF5
objects.
Besides, having H5close() in a file reader's destructor will close the
library for each file. Should you have more than one file in your app,
such is likely to lead to the crashes described before.
Werner
On Thu, 14 May 2009 04:54:24 -0500, Ramakrishnan Iyer >> <ramakrishnan.iyer@altair.com> wrote:
All,
I have a hdf5 reader . I load a hdf5 file . After Loading the hdf5 file
, the file is close using H5Fclose . In the destructor of the Reader I
have a call to H5garbage_collect . Now I want to delete the hdf5 file .
But I am not able to delete the file unless , I have a call to H5close()
in the Reader's destructor . Why is it so ?
Regards
Ram
--
___________________________________________________________________________
Dr. Werner Benger <werner@cct.lsu.edu> Visualization
Research
Laboratory for Creative Arts and Technology (LCAT)
Center for Computation & Technology at Louisiana State University (CCT/LSU)
239 Johnston Hall, Baton Rouge, Louisiana 70803
Tel.: +1 225 578 4809 Fax.: +1 225 578-5362
----------------------------------------------------------------------
This mailing list is for HDF software users discussion.
To subscribe to this list, send a message to
hdf-forum-subscribe@hdfgroup.org.
To unsubscribe, send a message to hdf-forum-unsubscribe@hdfgroup.org.