From b98a74ab1d2e5bb531f8926568f0cf74be25b4cf Mon Sep 17 00:00:00 2001 From: Steffen Schuemann Date: Wed, 6 Mar 2019 06:53:17 +0100 Subject: [PATCH] directory_iterator on Windows should free resources when reaching end(), #8 --- filesystem.h | 2 ++ test/filesystem_test.cpp | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/filesystem.h b/filesystem.h index 49529a6..e387442 100644 --- a/filesystem.h +++ b/filesystem.h @@ -4327,6 +4327,8 @@ class directory_iterator::impl copyToDirEntry(ec); } else { + FindClose(_dirHandle); + _dirHandle = INVALID_HANDLE_VALUE; _current = filesystem::path(); break; } diff --git a/test/filesystem_test.cpp b/test/filesystem_test.cpp index aa609ee..11d5276 100644 --- a/test/filesystem_test.cpp +++ b/test/filesystem_test.cpp @@ -1039,6 +1039,18 @@ TEST_CASE("30.10.13 class directory_iterator", "[filesystem][directory_iterator] CHECK(iter->file_size() == 1234); CHECK(++iter == fs::directory_iterator()); } + { + // Issue #8: check if resources are freed when iterator reaches end() + TemporaryDirectory t(TempOpt::change_path); + auto p = fs::path("test/"); + fs::create_directory(p); + auto iter = fs::directory_iterator(p); + while (iter != fs::directory_iterator()) { + ++iter; + } + CHECK(fs::remove_all(p) == 1); + CHECK_NOTHROW(fs::create_directory(p)); + } } TEST_CASE("30.10.14 class recursive_directory_iterator", "[filesystem][recursive_directory_iterator]")