tags:

views:

72

answers:

2

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
+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:

  1. The result code is set to NoError.
  2. A global cache is checked to see if your file is present
  3. Your file isn't present the first time, so it's parsed on qsettings.cpp line 1530 (Qt-4.6.2)
  4. Parsing results in an error and the result code is set (see qsettings.cpp line 1552).
  5. The error result code is returned.

And the second run is different:

  1. The result code is set to NoError.
  2. A global cache is checked, your file is present.
  3. The file size and timestamp are checked to see if the file has changed (see qsettings.cpp line 1424).
  4. The result code is returned, which happens to be NoError -- the file was assumed to have been parsed correctly.
Kaleb Pederson