Logan West
Logan West

Reputation: 45

Leaving ostream Open

I created a logger within my C++ program. Based on the code below, are there any issues I should be aware of? I'm mainly concerned with leaving the ostream open for the duration of the program and exiting the program through unclean methods (i.e. x'ing out of the console window). Will the system keep the file open beyond program exit, or will the ostream object be destroyed upon termination (not using the Log::Close() function).

My train of thought is: why open/close it over and over when I could just open it once and close it upon exit?

#ifndef LOG_HPP
#define LOG_HPP

#include <fstream>

namespace Log
{
    static const char* File = "Logs\\Log.log";
    static std::ofstream Log;

    void Initialize()
    {
        Log.open(File, std::ios::app);
    }

    void Record(const char* Message)
    {       
        Log << Message;
    }

    void Close()
    {
        Log.close();
    }

}; // namespace Log

#endif

Upvotes: 2

Views: 404

Answers (3)

armagedescu
armagedescu

Reputation: 2155

I would not care that much of the closed file handle. As Klaus said, is more important to flush the stream after writing each message.
On Windows you always can use the SEH frame, __try/__finally block. No matter how you exit the __try, the __finally will be executed, even after throws or return. I would expect you to get similar results with try/catch, just don't use return inside try block. Or take a look here:
__try/__finally equivalent in UNIX

Upvotes: 1

Klaus
Klaus

Reputation: 25663

Will the system keep the file open beyond program exit

No! After the program exits, independent if it exit in "normal" fashion or will be killed by any OS command or console control sequence, nearly all resources of the program will be freed.

As I know the only not fully removed resource is a named pipe. Normal file handles will definitely be dropped.

But: It is possible that the last buffer content will not be written to the physical file on disk or wherever you write to. For that reason it will be a good idea to flush() directly after each write.

Upvotes: 3

R Sahu
R Sahu

Reputation: 206717

The C++ standard does not define what happens when you exit the program through unclean methods (i.e. x'ing out of the console window).

However, it does define what to expect when you call std::abort.

Calling the function std::abort() declared in <cstdlib> terminates the program without executing any destructors and without calling the functions passed to std::atexit() or std::at_quick_exit().

The most likely outcome of exiting the program uncleanly is what you would expect to see when std::abort() gets called.

Will the system keep the file open beyond program exit

Even though destructors of objects that get called upon cleanly exiting the program do not get called in abnormal termination, the OS will most likely close open file handles. At least that's what I have observed in Windows and Linux. If there are any unflushed buffers associated with output files/streams, you will not see them flushed though.

Upvotes: 3

Related Questions