Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6919311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:58:25+00:00 2026-05-27T09:58:25+00:00

I’ve mostly used C for programming in the last 2 years (previously some Java)

  • 0

I’ve mostly used C for programming in the last 2 years (previously some Java) and have decided to learn C++, using Qt Creator and the Qt libraries.

My question is whether the following code introduces a memory leak:

    // filename is a QStringListIterator
    // dir is a QDir
    while (filename.hasNext()) {
            QString came_from_file(dir.filePath(filename.next()));
            QFile file(came_from_file);
            file.open(QFile::ReadOnly);
            file.readLine();
            while (!file.atEnd()) {
                    QString line(file.readLine());
                    do_something_with_stuff(line, came_from_file);
            }
    }

Specifically, I’m not sure what happens to the generated dir.filePath(filename.next()) QString. Is it referenced into came_from_file or is its pointer lost once it is copied? Is it “copied” (I assume it never is, until changed, due to the Copy-On-Write nature of Qt containers)? Should I write this differently, like QString match = dir.file...? To my understanding, this should be equal.

It also says in the Qt docs that QFile is going to close() the file if necessary in the destructor. Does the destructor get called? The variable does go “out of scope”, but I’m still not sure if this is a case of so-called RAII.

How would I make file point to a different file?

If I pass variables like this to functions (I assume this is by reference, as the function do_something... is defined this way), and then they go out of scope, but are insterted into a QHash/QMap/QSet by the function, what happens? Do they get removed and the containers go crazy, or is there some fancy little scheme like ref. counting behind it all? Or are the values simply copied?

I realise similiar questions have been asked before, but I can’t seem to figure it out in this example by reading them, as they seem to be different cases.

If there’s something wrong with the code or my understanding, please correct me. 🙂

Thanks,
Nanthiel

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:58:26+00:00Added an answer on May 27, 2026 at 9:58 am

    Specifically, I’m not sure what happens to the generated dir.filePath(filename.next()) QString.

    came_from_file is a copy (unless RVO kicks in) of the return value of QDir::filePath, which in turn is a temporary object that is automatically cleaned up. There is no need to assign it to a variable.

    Does the destructor get called? The variable does go “out of scope”

    When an object goes out of scope, its destructor will be called, unless you use unsafe constructs like longjmp.

    How would I make file point to a different file?

    I can’t find a clean way to do this in the Qt docs for QFile. I suggest you just declare a new QFile for the other file. One possibility to do that, and maintain control over the number of open files, is to use scopes:

    {
        QFile file(some_path);
        // do stuff to file
    }   // end of file's scope, so it will be closed
    
    {
        QFile file(other_path);
    }   // again, end of scope
    

    OTOH, you might want to explicitly close the QFile so that you can check its state after flush/close.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a reasonable size flat file database of text documents mostly saved in
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.