From aa54acd54da22e7e61acba345365f290b4dc5850 Mon Sep 17 00:00:00 2001 From: Vladislav Sovrasov Date: Mon, 7 Aug 2017 15:50:30 +0300 Subject: [PATCH 1/2] core: add a test to reproduce #9312 --- modules/core/test/test_io.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/modules/core/test/test_io.cpp b/modules/core/test/test_io.cpp index 4d3bb81066..b725dbcf23 100644 --- a/modules/core/test/test_io.cpp +++ b/modules/core/test/test_io.cpp @@ -1345,3 +1345,24 @@ TEST(Core_InputOutput, FileStorage_json_bool) ASSERT_EQ((int)fs["bool_false"], 0); fs.release(); } + +TEST(Core_InputOutput, FileStorage_free_file_after_exception) +{ + const std::string fileName = "test.yml"; + const std::string content = "%YAML:1.0\n cameraMatrix;:: !\n"; + + fstream testFile; + testFile.open(fileName, std::fstream::out); + if(!testFile.is_open()) FAIL(); + testFile << content; + testFile.close(); + + try + { + FileStorage fs(fileName, FileStorage::READ + FileStorage::FORMAT_YAML); + } + catch (const std::exception&) + { + ASSERT_EQ(std::remove(fileName.c_str()), 0); + } +} From 5e68b28ad320c6f65b38b811652178f7f3c49bb4 Mon Sep 17 00:00:00 2001 From: Vladislav Sovrasov Date: Mon, 7 Aug 2017 16:20:38 +0300 Subject: [PATCH 2/2] core: fix file not closed when exception in FS --- modules/core/src/persistence.cpp | 1 + modules/core/test/test_io.cpp | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/modules/core/src/persistence.cpp b/modules/core/src/persistence.cpp index 3f12fc42e7..e2d223d4b0 100644 --- a/modules/core/src/persistence.cpp +++ b/modules/core/src/persistence.cpp @@ -4525,6 +4525,7 @@ cvOpenFileStorage( const char* query, CvMemStorage* dststorage, int flags, const } catch (...) { + fs->is_opened = true; cvReleaseFileStorage( &fs ); throw; } diff --git a/modules/core/test/test_io.cpp b/modules/core/test/test_io.cpp index b725dbcf23..a70b2aac98 100644 --- a/modules/core/test/test_io.cpp +++ b/modules/core/test/test_io.cpp @@ -1352,7 +1352,7 @@ TEST(Core_InputOutput, FileStorage_free_file_after_exception) const std::string content = "%YAML:1.0\n cameraMatrix;:: !\n"; fstream testFile; - testFile.open(fileName, std::fstream::out); + testFile.open(fileName.c_str(), std::fstream::out); if(!testFile.is_open()) FAIL(); testFile << content; testFile.close(); @@ -1360,9 +1360,10 @@ TEST(Core_InputOutput, FileStorage_free_file_after_exception) try { FileStorage fs(fileName, FileStorage::READ + FileStorage::FORMAT_YAML); + FAIL(); } catch (const std::exception&) { - ASSERT_EQ(std::remove(fileName.c_str()), 0); } + ASSERT_EQ(std::remove(fileName.c_str()), 0); }