From 6b8ebf3e94dccd2c54056b6b8eee64ea6d99dbcd Mon Sep 17 00:00:00 2001 From: Roman Donchenko Date: Tue, 9 Dec 2014 19:13:19 +0300 Subject: [PATCH] Fix memory leaks appearing when cvOpenFileStorage throws (cherry picked from commit 16ce114e0cad6c85efead4a0ebb07724d691407a) --- modules/core/src/persistence.cpp | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/modules/core/src/persistence.cpp b/modules/core/src/persistence.cpp index 5e14e81aa..5c8fd2f97 100644 --- a/modules/core/src/persistence.cpp +++ b/modules/core/src/persistence.cpp @@ -2692,7 +2692,10 @@ cvOpenFileStorage( const char* filename, CvMemStorage* dststorage, int flags, co (dot_pos[3] == '\0' || (cv_isdigit(dot_pos[3]) && dot_pos[4] == '\0')) ) { if( append ) + { + cvReleaseFileStorage( &fs ); CV_Error(CV_StsNotImplemented, "Appending data to compressed file is not implemented" ); + } isGZ = true; compression = dot_pos[3]; if( compression ) @@ -2713,6 +2716,7 @@ cvOpenFileStorage( const char* filename, CvMemStorage* dststorage, int flags, co if( !fs->gzfile ) goto _exit_; #else + cvReleaseFileStorage( &fs ); CV_Error(CV_StsNotImplemented, "There is no compressed file storage support in this configuration"); #endif } @@ -2765,7 +2769,10 @@ cvOpenFileStorage( const char* filename, CvMemStorage* dststorage, int flags, co if( strcmp( encoding, "UTF-16" ) == 0 || strcmp( encoding, "utf-16" ) == 0 || strcmp( encoding, "Utf-16" ) == 0 ) + { + cvReleaseFileStorage( &fs ); CV_Error( CV_StsBadArg, "UTF-16 XML encoding is not supported! Use 8-bit encoding\n"); + } CV_Assert( strlen(encoding) < 1000 ); char buf[1100]; @@ -2802,7 +2809,10 @@ cvOpenFileStorage( const char* filename, CvMemStorage* dststorage, int flags, co } } if( last_occurence < 0 ) + { + cvReleaseFileStorage( &fs ); CV_Error( CV_StsError, "Could not find in the end of file.\n" ); + } icvCloseFile( fs ); fs->file = fopen( fs->filename, "r+t" ); fseek( fs->file, last_occurence, SEEK_SET ); @@ -2876,10 +2886,18 @@ cvOpenFileStorage( const char* filename, CvMemStorage* dststorage, int flags, co //mode = cvGetErrMode(); //cvSetErrMode( CV_ErrModeSilent ); - if( fs->fmt == CV_STORAGE_FORMAT_XML ) - icvXMLParse( fs ); - else - icvYMLParse( fs ); + try + { + if( fs->fmt == CV_STORAGE_FORMAT_XML ) + icvXMLParse( fs ); + else + icvYMLParse( fs ); + } + catch (...) + { + cvReleaseFileStorage( &fs ); + throw; + } //cvSetErrMode( mode ); // release resources that we do not need anymore