Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. The proper way to close a QFile?
Forum Updated to NodeBB v4.3 + New Features

The proper way to close a QFile?

Scheduled Pinned Locked Moved Solved General and Desktop
qfilememory leak
3 Posts 3 Posters 2.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    legitnameyo
    wrote on last edited by legitnameyo
    #1
    QFile example_file('/usr/test/file.txt');
    
    if(example_file.open(QFile::ReadOnly | QFile::Text) {
        QString line = example_file.readLine();
        qDebug() << "line:" << line;
    }
    
    example_file.flush(); // redundant?
    example_file.close(); // is this needed if I flush or does flush take care of memory leaks already?
    

    How do I close a file I open in with QFile? Does flush take care of memory leaks or is it useless to use flush when I will use 'close' directly after anyways?

    JonBJ 1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      Do you understand that flushing the file isn't relevant when reading? Flush doesn't have anything to do with memory leaks.

      But to your direct question:
      See the documentation for the destructor QFile::~QFile() It implicitly closes the file if it is open. Since your object is local (stack allocated) the destructor will automatically be executed when the object goes out of scope.

      1 Reply Last reply
      8
      • L legitnameyo
        QFile example_file('/usr/test/file.txt');
        
        if(example_file.open(QFile::ReadOnly | QFile::Text) {
            QString line = example_file.readLine();
            qDebug() << "line:" << line;
        }
        
        example_file.flush(); // redundant?
        example_file.close(); // is this needed if I flush or does flush take care of memory leaks already?
        

        How do I close a file I open in with QFile? Does flush take care of memory leaks or is it useless to use flush when I will use 'close' directly after anyways?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @legitnameyo
        Although what @Kent-Dorfman says about stack-allocated destructor auto-closing is quite correct, I would always put in an explicit close() as soon as I am finished with any file, personally. It's good practice, especially for potential future code changes. You never need flush() on read, and you don't need flush() on write if it's immediately followed by close().

        1 Reply Last reply
        3

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved