Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Qt auto close files?

Tags:

c++

qt

qfile

One of the QIODevice reimplemented open() methods in QFile has a QFileDevice::FileHandleFlag argument. Taking a look at the documentation for it there are two options with contradicting descriptions.

From the QFileDevice documentation:

  • QFileDevice::AutoCloseHandle – The file handle passed into open() should be closed by close(), the default behavior is that close just flushes the file and the application is responsible for closing the file handle. When opening a file by name, this flag is ignored as Qt always owns the file handle and must close it.

  • QFileDevice::DontCloseHandle – If not explicitly closed, the underlying file handle is left open when the QFile object is destroyed.

So does Qt auto close files or not and does setting this option actually change anything?

like image 829
Itay Grudev Avatar asked Jul 02 '16 20:07

Itay Grudev


1 Answers

After looking up the Qt source I found the line in QFSFileEngine.cpp:378* that ultimately uses the flag.

QFile::open() can be passed an existing (stdio.h) FILE handler which was not created by Qt and shouldn't be automatically closed by Qt. In contrast files opened by Qt are automatically closed by Qt.

The QFileDevice::FileHandleFlag flag is for the former case and allows the programmer to specify if QFile should auto-close a file ignoring the fact that it was not opened by Qt.


* Search for closeFileHandle if the line number doesn't match.

like image 113
Itay Grudev Avatar answered Oct 30 '22 12:10

Itay Grudev