diff --git a/modules/core/include/opencv2/core/persistence.hpp b/modules/core/include/opencv2/core/persistence.hpp index 73ba2fcb3eac3f2e23590e40b3d4190f2d514bba..f5687614bbd8c808805ec584a33addf74ea5346c 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 a21ee5a3615da43bcc17c167bd04707bc58d2821..39ce6331397adbc1c0827ab5e5498b75f501deda 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 c11c23598dd9c6ca413062dce484737a73a0b225..b49eb913161850dde5ccd8dd6d04b252f8b58a4c 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 80b5972597129c10dff4d75d62e2b73d33e9d76a..5cc7f6a61b0e1556fed9d778b971078fe68dbce1 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)); } }