-1

I want to write some logs in file.

This is my Code:

FILE *writefile = fopen((Dir + FileName).data(), "at");
if (writefile)
{
    fprintf(writefile, log.data());
    fclose(writefile);
}

It surely works the first time when I try to write.

This code is executed in networking system, but it is executed in no connection status.

So, it keeps writing log "Try Connct" in every sec.

This is the image what I want to write

I can see these log on my console.

In image, I try to write log over 11:10:00.

But in File, it was not written until 11:06:00.

I try to debug my code,

This is the image in debugging

Why this happens?

---Addition---

I try to print error. So, I used perror.

I got Error : Too many open files.

But Why? Obviously I closed file, as you can see in my code.

Retalia K
  • 64
  • 9
  • 1
    I don't know why `fopen()` is failing, but you can get a hint about why it fails by checking the value of `errno` immediately after `fopen()` returns NULL (or, perhaps more conveniently, by calling perror("fopen") at that point, to see what error-message gets printed) – Jeremy Friesner Mar 25 '19 at 02:36
  • @JeremyFriesner Okay, I'll try to print error. Thanks – Retalia K Mar 25 '19 at 02:42
  • Maybe one of `Dir` or `FileName` have changed. Maybe your antivirus is getting in the way. – drescherjm Mar 25 '19 at 02:51
  • I got Error : Too many open files. But Why? Obviously I closed file As you can see in my code. – Retalia K Mar 25 '19 at 03:00
  • You close the file in this one place. Maybe you don't close it somewhere else. – drescherjm Mar 25 '19 at 03:02
  • @drescherjm Obviously, there is no other place where I used "fopen". – Retalia K Mar 25 '19 at 03:06
  • 1
    Your error might just be a symptom of an underlying problem elsewhere in your code. Try to create a [MCVE], as right now we can only poke in the dark and do guess work. Also please don't screenshot error messages, debug information or code. Include any textual information as text, not as image. Thanks! – Max Vollmer Mar 25 '19 at 03:06
  • The reason I ask is we can't see the rest of the code. The bug is likely elsewhere. – drescherjm Mar 25 '19 at 03:07
  • "Too many open files" is an error that refers to system wide file handles, so the problem can even be caused by other processes. File handles can include network handles as well, since you mentioned you are in a networking environment, this is also something to look out for. – Max Vollmer Mar 25 '19 at 03:07
  • I solved this problem. It's not fopen's issue. I opened socket, and shutdown, but not close... Thanks u guys. – Retalia K Mar 27 '19 at 09:07

1 Answers1

0

You have a resource leak in your program. There are too many open/dup/fopen/stream::open/pipe/connect/accept/socket calls without a matching close/fclose/~stream/stream::close.

In the case of FILE leak, you can avoid it by one of:

  • use ofstream instead of FILE in all your code. It will automatically close the file when it gets out of scope.
  • use unique_ptr of FILE with a custom deleter. This will close the file automatically, when it gets out of scope.

Unfortunately there is no easy way to find this leak. The best way is to avoid it from the beginning, by using RAII all the way with std::stream, std::unique_ptr, and boost asio. Since the code didn't follow this rule, the only way out is to search for fopen (and others) through the code, and make it use RAII, possibly with unique_ptr.

Michael Veksler
  • 8,217
  • 1
  • 20
  • 33