mirror of
https://github.com/opencv/opencv.git
synced 2025-06-07 09:25:45 +08:00
core(persistence): fix resource leaks - force closing files
This commit is contained in:
parent
4dfa798e75
commit
673eb2b006
@ -407,14 +407,13 @@ public:
|
|||||||
else if ( fmt == FileStorage::FORMAT_JSON )
|
else if ( fmt == FileStorage::FORMAT_JSON )
|
||||||
puts( "}\n" );
|
puts( "}\n" );
|
||||||
}
|
}
|
||||||
|
|
||||||
closeFile();
|
|
||||||
if( mem_mode && out )
|
if( mem_mode && out )
|
||||||
{
|
{
|
||||||
*out = cv::String(outbuf.begin(), outbuf.end());
|
*out = cv::String(outbuf.begin(), outbuf.end());
|
||||||
}
|
}
|
||||||
init();
|
|
||||||
}
|
}
|
||||||
|
closeFile();
|
||||||
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
void analyze_file_name( const std::string& file_name, std::vector<std::string>& params )
|
void analyze_file_name( const std::string& file_name, std::vector<std::string>& params )
|
||||||
@ -1825,10 +1824,18 @@ FileStorage::~FileStorage()
|
|||||||
|
|
||||||
bool FileStorage::open(const String& filename, int flags, const String& encoding)
|
bool FileStorage::open(const String& filename, int flags, const String& encoding)
|
||||||
{
|
{
|
||||||
bool ok = p->open(filename.c_str(), flags, encoding.c_str());
|
try
|
||||||
if(ok)
|
{
|
||||||
state = FileStorage::NAME_EXPECTED + FileStorage::INSIDE_MAP;
|
bool ok = p->open(filename.c_str(), flags, encoding.c_str());
|
||||||
return ok;
|
if(ok)
|
||||||
|
state = FileStorage::NAME_EXPECTED + FileStorage::INSIDE_MAP;
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
release();
|
||||||
|
throw; // re-throw
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FileStorage::isOpened() const { return p->is_opened; }
|
bool FileStorage::isOpened() const { return p->is_opened; }
|
||||||
|
@ -1711,4 +1711,65 @@ TEST(Core_InputOutput, FileStorage_JSON_VeryLongLines)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(Core_InputOutput, FileStorage_empty_16823)
|
||||||
|
{
|
||||||
|
std::string fname = tempfile("test_fs_empty.yml");
|
||||||
|
{
|
||||||
|
// create empty file
|
||||||
|
std::ofstream f(fname.c_str(), std::ios::out);
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
FileStorage fs(fname, FileStorage::READ);
|
||||||
|
ADD_FAILURE() << "Exception must be thrown for empty file.";
|
||||||
|
}
|
||||||
|
catch (const cv::Exception&)
|
||||||
|
{
|
||||||
|
// expected way
|
||||||
|
// closed files can be checked manually through 'strace'
|
||||||
|
}
|
||||||
|
catch (const std::exception& e)
|
||||||
|
{
|
||||||
|
ADD_FAILURE() << "Unexpected exception: " << e.what();
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
ADD_FAILURE() << "Unexpected unknown C++ exception";
|
||||||
|
}
|
||||||
|
|
||||||
|
EXPECT_EQ(0, remove(fname.c_str()));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(Core_InputOutput, FileStorage_open_empty_16823)
|
||||||
|
{
|
||||||
|
std::string fname = tempfile("test_fs_open_empty.yml");
|
||||||
|
{
|
||||||
|
// create empty file
|
||||||
|
std::ofstream f(fname.c_str(), std::ios::out);
|
||||||
|
}
|
||||||
|
|
||||||
|
FileStorage fs;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
fs.open(fname, FileStorage::READ);
|
||||||
|
ADD_FAILURE() << "Exception must be thrown for empty file.";
|
||||||
|
}
|
||||||
|
catch (const cv::Exception&)
|
||||||
|
{
|
||||||
|
// expected way
|
||||||
|
// closed files can be checked manually through 'strace'
|
||||||
|
}
|
||||||
|
catch (const std::exception& e)
|
||||||
|
{
|
||||||
|
ADD_FAILURE() << "Unexpected exception: " << e.what();
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
ADD_FAILURE() << "Unexpected unknown C++ exception";
|
||||||
|
}
|
||||||
|
|
||||||
|
EXPECT_EQ(0, remove(fname.c_str()));
|
||||||
|
}
|
||||||
|
|
||||||
}} // namespace
|
}} // namespace
|
||||||
|
Loading…
Reference in New Issue
Block a user