Opened 9 years ago

#9597 new Bugs

"boost::iostreams::file_descriptor" doesn't close Windows low-level file descriptors properly when specifying "file_descriptor_flags::close_handle"

Reported by: anonymous Owned by: Jonathan Turkanis
Milestone: To Be Determined Component: iostreams
Version: Boost 1.55.0 Severity: Problem
Keywords: file_descriptor Cc: m.kosch@…

Description

When opening a file on Windows using boost::iostreams::file_descriptor, boost::iostreams::file_descriptor_sink or boost::iostreams::file_descriptor_source in combination with a low-level file descriptor and when specifying file_descriptor_flags::close_handle, then the file descriptor isn't closed properly on close/exit.

Using one of the mentioned methods, the low-level file descriptor is converted to a Windows handle using the API call _get_osfhandle (1):

void file_descriptor_impl::open(int fd, flags f)
{ open(reinterpret_cast<file_handle>(_get_osfhandle(fd)), f); }

When closing, the file handle is closed using CloseHandle:

void file_descriptor_impl::close_impl(bool close_flag, bool throw_) {
    if (handle_ != invalid_handle()) {
        if (close_flag) {
            bool success = 
                #ifdef BOOST_IOSTREAMS_WINDOWS
                    ::CloseHandle(handle_) == 1;
                #else
                    BOOST_IOSTREAMS_FD_CLOSE(handle_) != -1;
                #endif
            if (!success && throw_)
                throw_system_failure("failed closing file");
        }
        handle_ = invalid_handle();
        flags_ = 0;
    }
}

According to (1) the API call _close (2) shall be used to close a low-level file descriptor. Practical tests shew that every time a file is opened and closed using one of the mentioned methods, Windows (XP) assigns a new file descriptor with an increasing value to it, unless the program runs out of available file handles. Manually closing the file by specifying file_descriptor_flags::close_handle and calling _close solves this issue.

(1) http://msdn.microsoft.com/en-us/library/ks2530z6.aspx
(2) http://msdn.microsoft.com/en-us/library/5fzwd5ss.aspx

Change History (0)

Note: See TracTickets for help on using tickets.