From 73d03d81af9b694b2901af850015e75a5b9bd1b5 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Thu, 24 Aug 2023 13:56:41 +0200 Subject: [PATCH] stream: fix buffered_sys and cached_file destruction We had to introduce out-of-order construction for those classes, but we forgot to implement out-of-order destruction. File gets closed first by cached_file::m_source destructor, then inherited destructor cache::~cache() which flushes the cache is called. Signed-off-by: Simon Rozman --- include/stdex/stream.hpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/include/stdex/stream.hpp b/include/stdex/stream.hpp index 1e65d1c2a..6bddaa911 100644 --- a/include/stdex/stream.hpp +++ b/include/stdex/stream.hpp @@ -858,6 +858,11 @@ namespace stdex m_source = &source; } + void done() + { + m_source = nullptr; + } + public: converter(_Inout_ basic& source) : basic(source.state()), @@ -1215,6 +1220,13 @@ namespace stdex m_write_buffer(write_buffer_size) {} + void done() + { + if (m_source) + flush_write(); + converter::done(); + } + public: buffer(_Inout_ basic& source, _In_ size_t read_buffer_size = default_buffer_size, _In_ size_t write_buffer_size = default_buffer_size) : converter(source), @@ -1651,6 +1663,17 @@ namespace stdex #endif } + void done() + { + if (m_source) { + flush_cache(); + if (!ok()) _Unlikely_ + throw std::runtime_error("cache flush failed"); // Data loss occured + m_source->seek(m_offset); + m_source = nullptr; + } + } + public: cache(_Inout_ basic_file& source, _In_ size_t cache_size = default_cache_size) : basic(source.state()), @@ -2135,6 +2158,11 @@ namespace stdex init(m_source); } + virtual ~buffered_sys() + { + done(); + } + protected: basic_sys m_source; }; @@ -2697,6 +2725,11 @@ namespace stdex init(m_source); } + virtual ~cached_file() + { + done(); + } + /// /// Opens file ///