From 9a6612c6ece02e37e1ce8d9d0e7f84e435e3314a Mon Sep 17 00:00:00 2001 From: Dick Hollenbeck Date: Wed, 18 Apr 2012 07:24:40 -0500 Subject: [PATCH] use a block scope to invoke wxFFile's destructor before renaming temporary file --- pcbnew/legacy_plugin.cpp | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/pcbnew/legacy_plugin.cpp b/pcbnew/legacy_plugin.cpp index 52476367ac..9e944698d6 100644 --- a/pcbnew/legacy_plugin.cpp +++ b/pcbnew/legacy_plugin.cpp @@ -3933,23 +3933,30 @@ void FPL_CACHE::Save() wxString tempFileName = wxFileName::CreateTempFileName( m_lib_name ); - wxLogDebug( "tempFileName:'%s'\n", TO_UTF8( tempFileName ) ); + // wxLogDebug( "tempFileName:'%s'\n", TO_UTF8( tempFileName ) ); - FILE* fp = wxFopen( tempFileName, wxT( "w" ) ); - if( !fp ) + // a block {} scope to fire wxFFile wxf()'s destructor { - THROW_IO_ERROR( wxString::Format( - _( "Unable to open or create legacy library file '%s'" ), - m_lib_name.GetData() ) ); + FILE* fp = wxFopen( tempFileName, wxT( "w" ) ); + if( !fp ) + { + THROW_IO_ERROR( wxString::Format( + _( "Unable to open or create legacy library file '%s'" ), + m_lib_name.GetData() ) ); + } + + // wxf now owns fp, will close on exception or exit from + // this block {} scope + wxFFile wxf( fp ); + + SaveHeader( fp ); + SaveIndex( fp ); + SaveModules( fp ); + SaveEndOfFile( fp ); } - // wxf now owns fp, will close on exception or return - wxFFile wxf( fp ); - - SaveHeader( fp ); - SaveIndex( fp ); - SaveModules( fp ); - SaveEndOfFile( fp ); + // fp is now closed here, and that seems proper before trying to rename + // the temporary file to m_lib_name. wxRemove( m_lib_name ); // it is not an error if this does not exist