473,320 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

destructor and file descriptors

I fixed a bug today that went against my intuition. I am on Linux.
I had a class that fopen'ed some files. When I called delete
on these objects, I expected that the files would be closed
automatically, just as when an application terminates, but
evidently this is not the case. I guess I have to fclose the files
in the destructor.

Is this a correct assessment?

Jan 23 '07 #1
5 4046
"Digital Puer" <di**********@hotmail.comwrote:
I fixed a bug today that went against my intuition. I am on Linux.
I had a class that fopen'ed some files. When I called delete
on these objects, I expected that the files would be closed
automatically, just as when an application terminates, but
evidently this is not the case. I guess I have to fclose the files
in the destructor.

Is this a correct assessment?
Yes. If you use fstream instead, they will automatically close.
Jan 23 '07 #2
Digital Puer <di**********@hotmail.comwrote:
I fixed a bug today that went against my intuition. I am on Linux.
I had a class that fopen'ed some files. When I called delete
on these objects, I expected that the files would be closed
automatically, just as when an application terminates, but
evidently this is not the case. I guess I have to fclose the files
in the destructor.

Is this a correct assessment?
I would say yes. However, if you used std::fstream objects instead of
FILE*, then the fstream destructors would handle closing the files, so
you wouldn't need to manually close them in your destructor.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jan 23 '07 #3
On 22 Jan 2007 18:29:26 -0800, "Digital Puer" wrote:
>I fixed a bug today that went against my intuition. I am on Linux.
I had a class that fopen'ed some files. When I called delete
on these objects, I expected that the files would be closed
automatically, just as when an application terminates, but
evidently this is not the case. I guess I have to fclose the files
in the destructor.

Is this a correct assessment?
Merely calling fclose() in the destructor is not the right solution
when the file has been opened for writing. You need to check the
return value of fclose() and conduct appropriate action when fclose()
fails. Therefore the place for 'successful' fclose() is not the
destructor but another member function. In the destructor you can only
perform rollback, undo or cleanup activities which may include a
fclose() that terminates the unsuccessful file operation. The
situation is diffenent when the file has been opened for read only. In
that case you can put fclose() in the destructor only and ignore the
return value.

Best wishes,
Roland Pibinger
Jan 23 '07 #4
rp*****@yahoo.com (Roland Pibinger) wrote:
Merely calling fclose() in the destructor is not the right solution
when the file has been opened for writing. You need to check the
return value of fclose() and conduct appropriate action when
fclose() fails.
Could you post some more details on this one? What kind of actions is
appropriate when fclose fails? I ask because basic_fstream's close
function returns void. Presumably, it does the appropriate action you
speak of?
Jan 23 '07 #5
On Tue, 23 Jan 2007 23:14:43 GMT, "Daniel T." wrote:
>rpbg123@...com (Roland Pibinger) wrote:
>Merely calling fclose() in the destructor is not the right solution
when the file has been opened for writing. You need to check the
return value of fclose() and conduct appropriate action when
fclose() fails.

Could you post some more details on this one? What kind of actions is
appropriate when fclose fails?
This is more a design than an implementation question. One common
setting is that the whole file is read into memory, some changes are
made and the contents is written back to disk by overwriting the
original file. When fwrite() or fclose() fails the original file is
already destroyed and the new file is corrupt (half written).
One solution is that the original file is never overwritten, i.e.
never opened for write. This means that you write the contents to a
temporary file, close the temporary file (check the return value),
delete the original file and rename the temporary file to the original
file. Error-/failure-handling in that case just means that the
partially written temporary file is closed and deleted (the original
file remians unchanged). This kind of error-/failure-handling can also
be done in the destructor when the file has not been closed before
(the destructor performs only error-/failure-handling).
>I ask because basic_fstream's close
function returns void. Presumably, it does the appropriate action you
speak of?
AFAIK, fstream() has functions like good(), bad(), fail(), ... to let
the user ckeck the stream state.

Best wishes,
Roland Pibinger

Jan 24 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: François Pinard | last post by:
This question is a bit technical, but hopefully, this list will offer me good hints or nice solutions. Happily enough for me, it often does! :-) I would need to recognise and play with...
5
by: John Marshall | last post by:
Hi, Does anyone see a problem with doing: data = file("tata").read() Each time this is done, I see a new file descriptor allocated (Linux) but not released. 1) Will there ever be a point...
5
by: JG | last post by:
Hi all, Does anyone know how the implementations on Linux and Windows handle synchronization between a read and write FD open to the same file. For example, if I have 2 FD open to file X.txt. ...
36
by: Roman Mashak | last post by:
Hello, All! I implemented simple program to eliminate entry from the file having the following structure (actually it's config file of 'named' DNS package for those who care and know): ...
0
by: jfigueiras | last post by:
>I have a problem with the module subprocess! As many other programs... I'm not sure what you mean by "non-standard file descriptors". The other program is free to open, read, write, etc any...
1
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this...
5
by: yinglcs | last post by:
I have a c/c++ program in linux. I would like to know if I kill my program (e.g. exit(1)), will it release all the file descriptors my program held (regardless if I call close(fd) of each file...
8
ashitpro
by: ashitpro | last post by:
Understanding Ext-2 file system:chapter 1 The first block in each Ext2 partition is never managed by the Ext2 filesystem, because it is reserved for the partition boot sector. The rest of the...
2
by: DJ Dharme | last post by:
Hi all, I am writing a multi-threaded application in c++ running on solaris. I have a file which is updated by a single thread by appending data into the file and at same time the other threads...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.