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.7k 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 9 Jul 2019, 03:49 last edited by legitnameyo 7 Sept 2019, 03:51
    #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?

    J 1 Reply Last reply 9 Jul 2019, 08:23
    0
    • K Offline
      K Offline
      Kent-Dorfman
      wrote on 9 Jul 2019, 04:14 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
        9 Jul 2019, 03:49
        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?

        J Offline
        J Offline
        JonB
        wrote on 9 Jul 2019, 08:23 last edited by JonB 7 Sept 2019, 08:25
        #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

        3/3

        9 Jul 2019, 08:23

        • Login

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