Skip to content

Instantly share code, notes, and snippets.

@hdclark
Created July 20, 2012 17:52
Show Gist options
  • Save hdclark/3152227 to your computer and use it in GitHub Desktop.
Save hdclark/3152227 to your computer and use it in GitHub Desktop.
popen Segfault
....
FILE *fp;
....
while( true ){
if(fp == nullptr) pclose(fp); // <---seg. fault happens here. (see below)
// or
//if(fp == 0) pclose(fp);
// or
//if(fp == NULL) pclose(fp);
...
fp = popen(...);
...
pclose(fp);
//-------------------------------------------------------------------------------
// NOTE: The above segfault will evaporate if you manually nullify fp.
//-------------------------------------------------------------------------------
fp = nullptr; // <---- This will fix the above segfault.
//-------------------------------------------------------------------------------
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment