I have looked into below question, where I understood that I don't have to close ifstream
since the the file handler is closed automatically when exits from the scope. [ defenitely there is no memory leak]
[Do I need to manually close an ifstream?1
But in the below code, the file handler is a global variable. So if we dont close the file handler manually it will cause memory leak I think. Is that assumption corruct or the file handler will be automatically handled by c++ ?
#include <iostream>
#include <fstream>
#include <sstream>
#include <unistd.h>
using namespace::std;
ifstream config_file;
stringstream cmd;
int test() {
config_file.open("config.file");
cmd << config_file.rdbuf();
string tmp = cmd.str();
cout << " config buffer is " << tmp << "\n" <<endl;
}
int main () {
test();
while (1) {
test();
sleep(1);
}
}
Interestingly, cppcheck also didn't report a memory leak in this file.
cppcheck file.cpp
//no error logs.
Can someone please confirm that there is memory leak or not in the above code?