From b296e2d40f2d0d4048d8d7025d41d699228f007b Mon Sep 17 00:00:00 2001 From: mandreyel Date: Fri, 27 Dec 2019 18:25:47 +0100 Subject: [PATCH] Fix not releasing file handle on unsuccessful mapping on win32 --- include/mio/detail/mmap.ipp | 14 ++++++++------ single_include/mio/mio.hpp | 14 ++++++++------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/include/mio/detail/mmap.ipp b/include/mio/detail/mmap.ipp index 361db30..aaf6494 100644 --- a/include/mio/detail/mmap.ipp +++ b/include/mio/detail/mmap.ipp @@ -183,6 +183,8 @@ inline mmap_context memory_map(const file_handle_type file_handle, const int64_t length_to_map)); if(mapping_start == nullptr) { + // Close file handle if mapping it failed. + ::CloseHandle(file_mapping_handle); error = detail::last_error(); return {}; } @@ -438,14 +440,14 @@ void basic_mmap::swap(basic_mmap& other) if(this != &other) { using std::swap; - swap(data_, other.data_); - swap(file_handle_, other.file_handle_); + swap(data_, other.data_); + swap(file_handle_, other.file_handle_); #ifdef _WIN32 - swap(file_mapping_handle_, other.file_mapping_handle_); + swap(file_mapping_handle_, other.file_mapping_handle_); #endif - swap(length_, other.length_); - swap(mapped_length_, other.mapped_length_); - swap(is_handle_internal_, other.is_handle_internal_); + swap(length_, other.length_); + swap(mapped_length_, other.mapped_length_); + swap(is_handle_internal_, other.is_handle_internal_); } } diff --git a/single_include/mio/mio.hpp b/single_include/mio/mio.hpp index b4b8cd5..95c696c 100644 --- a/single_include/mio/mio.hpp +++ b/single_include/mio/mio.hpp @@ -925,6 +925,8 @@ inline mmap_context memory_map(const file_handle_type file_handle, const int64_t length_to_map)); if(mapping_start == nullptr) { + // Close file handle if mapping it failed. + ::CloseHandle(file_mapping_handle); error = detail::last_error(); return {}; } @@ -1180,14 +1182,14 @@ void basic_mmap::swap(basic_mmap& other) if(this != &other) { using std::swap; - swap(data_, other.data_); - swap(file_handle_, other.file_handle_); + swap(data_, other.data_); + swap(file_handle_, other.file_handle_); #ifdef _WIN32 - swap(file_mapping_handle_, other.file_mapping_handle_); + swap(file_mapping_handle_, other.file_mapping_handle_); #endif - swap(length_, other.length_); - swap(mapped_length_, other.mapped_length_); - swap(is_handle_internal_, other.is_handle_internal_); + swap(length_, other.length_); + swap(mapped_length_, other.mapped_length_); + swap(is_handle_internal_, other.is_handle_internal_); } }