diff --git a/modules/core/include/opencv2/core/persistence.hpp b/modules/core/include/opencv2/core/persistence.hpp index 73ba2fcb3e..f5687614bb 100644 --- a/modules/core/include/opencv2/core/persistence.hpp +++ b/modules/core/include/opencv2/core/persistence.hpp @@ -186,7 +186,7 @@ public: //! the full constructor that opens file storage for reading or writing CV_WRAP FileStorage(const String& source, int flags, const String& encoding=String()); //! the constructor that takes pointer to the C FileStorage structure - FileStorage(CvFileStorage* fs); + FileStorage(CvFileStorage* fs, bool owning=true); //! the destructor. calls release() virtual ~FileStorage(); diff --git a/modules/core/src/persistence.cpp b/modules/core/src/persistence.cpp index a21ee5a361..39ce633139 100644 --- a/modules/core/src/persistence.cpp +++ b/modules/core/src/persistence.cpp @@ -5129,9 +5129,11 @@ FileStorage::FileStorage(const String& filename, int flags, const String& encodi open( filename, flags, encoding ); } -FileStorage::FileStorage(CvFileStorage* _fs) +FileStorage::FileStorage(CvFileStorage* _fs, bool owning) { - fs = Ptr(_fs); + if (owning) fs.reset(_fs); + else fs = Ptr(Ptr(), _fs); + state = _fs ? NAME_EXPECTED + INSIDE_MAP : UNDEFINED; } diff --git a/modules/legacy/src/em.cpp b/modules/legacy/src/em.cpp index c11c23598d..b49eb91316 100644 --- a/modules/legacy/src/em.cpp +++ b/modules/legacy/src/em.cpp @@ -85,13 +85,12 @@ void CvEM::read( CvFileStorage* fs, CvFileNode* node ) void CvEM::write( CvFileStorage* _fs, const char* name ) const { - FileStorage fs = _fs; + FileStorage fs(_fs, false); if(name) fs << name << "{"; emObj.write(fs); if(name) fs << "}"; - fs.fs.obj = 0; } double CvEM::calcLikelihood( const Mat &input_sample ) const diff --git a/modules/objdetect/src/hog.cpp b/modules/objdetect/src/hog.cpp index 80b5972597..5cc7f6a61b 100644 --- a/modules/objdetect/src/hog.cpp +++ b/modules/objdetect/src/hog.cpp @@ -1353,8 +1353,7 @@ public: { if(ptr && _fs) { - FileStorage fs(_fs); - fs.fs.addref(); + FileStorage fs(_fs, false); ((const _ClsName*)ptr)->write(fs, String(name)); } }