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 3432986
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:31:06+00:00 2026-05-18T07:31:06+00:00

As the title says , I am unsure if I should close a stream

  • 0

As the title says , I am unsure if I should close a stream that was opened using popen.

The reason I am unsure is because every time i call pclose on a stream that was opened using popen I get a -1 return code.

If I make a call to perror after that I get the following message.

pclose: No Child Processes

The code that I am using below is basically to run a command and capture its output.I get the error from the last line (return pclose(fileListingStream);)

int executeCommand(char *command) {
    //The Stream to read that will contain the output of the command
    FILE *fileListingStream;
    char path[PATH_MAX];

    //Run the commmand in read mode
    fileListingStream = popen(command,"r");

    //Ensure that its not null before continuing
    if (fileListingStream == NULL)
        return EXIT_FAILURE;

    //Get the data from the stream and then print to the the console
    while (fgets(path, PATH_MAX, fileListingStream) != NULL)
        printf("%s", path);

    //Close the stream and return its return code
    return pclose(fileListingStream);
}
  • 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-18T07:31:06+00:00Added an answer on May 18, 2026 at 7:31 am

    Yes you should. See this answer for an explanation on the inner workings of pclose(). Furthermore you should note that errors in wait4() can be the cause of an apparent failure in pclose().

    Update0

    If the FILE * is valid (internally this is signified by the file descriptor not being -1), pclose() and fclose() will not cause leaks if there is an error. It’s worth noting that if the FILE * is not valid, there’s nothing to clean up anyway. As discussed in the answer I linked to, there is extra behaviour for pclose(), namely removing the FILE * from the proc file chain, and then waiting for the child to terminate. The internal wait is actually the second last thing done for a pclose(), everything has already been cleaned up by this point. Immediately after waiting, the contents of the FILE are trashed to signify its invalidity, this occurs regardless of any error in waitpid().

    Given the error you are receiving, ECHILD, I can definitively say that there is no memory leak for pclose() under eglibc-2.11.1, and likely any glibc-derived library for at least the past 1-4 years.

    If you wish to be completely certain, just run your program under valgrind, and trigger the ECHILD error. Valgrind will inform you if anything was leaked.

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

Sidebar

Related Questions

The title says it! I know that Jon Skeet was working on an implementation
The title says everything. I'm using Visual Studio Team System 2008 Database Edition and
As the title says i want to call a mixed mode assembly from unmanaged
As title says, how do I call a java class method from a jsp,
The title says everything. I am talking about C/C++ specifically, because both consider this
Title says all. I'll have navigation that looks like this and it's dynamically created
As the title says I'm facing a problem displaying the right left time to
Title says mainly the problem. On my spare time for our group at school,
As title says, should I just add a nullable keyword as a constraint?
like the title says, I am trying to create a drop down menu using

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.