I am using QSettings to try and figure out if an INI is valid.(using status() to check) I made a purposefully invalid INI file and loaded it in. The first time the code is called, it returns invalid, but every time after that, it returns valid. Is this a bug in my code?
A:
Checked your code, you need to delete the file object before returning.
Apart from that, your code uses the QSettings::QSettings(fileName, format)
c'tor to open an ini-file. That call ends in the function QConfFile::fromName
(implemented in qsettings.cpp). As I read it (there are a few macros and such that I decided not to follow), the file is not re-opened if the file already is open (i.e. you have not deleted the object since the last time). Thus the status will be ok the second time around.
e8johan
2010-02-19 07:24:36
+1
A:
It's a Qt bug caused by some global state. Note that the difference in results happens whether or not you call delete
on your QSettings object, which you should. Here's a brief summary of what happens on the first run:
- The result code is set to
NoError
. - A global cache is checked to see if your file is present
- Your file isn't present the first time, so it's parsed on
qsettings.cpp
line 1530 (Qt-4.6.2) - Parsing results in an error and the result code is set (see
qsettings.cpp
line 1552). - The error result code is returned.
And the second run is different:
- The result code is set to
NoError
. - A global cache is checked, your file is present.
- The file size and timestamp are checked to see if the file has changed (see
qsettings.cpp
line 1424). - The result code is returned, which happens to be
NoError
-- the file was assumed to have been parsed correctly.
Kaleb Pederson
2010-02-19 08:03:19