Search code examples
qt4

Do I really need to call QFile::close() in this case?


I'm not sure about QFile's behavior,

bool Class::Function (const QString & name)
{
  QFile fp (name);
  if (fp.open (QIODevice::ReadOnly))
  {
     // read file
     return false;
  }
  return true;
}

Hmm, it's not like a FILE* pointer in C (which you must close and free), would this be a problem if I don't call QFile::close() (Does it do automatically on destruction) ?


Solution

  • QFile object is closed automatically (Qt documentation) upon its destructor; hence there is no need to call close() explicitly.