From a9efbf47161bbbbf7ab6619f099d6b4eb3671451 Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Tue, 11 Dec 2018 16:21:21 +0100 Subject: [PATCH] Fixed memory leaks --- bitmap2component/bitmap2cmp_gui.cpp | 4 +--- common/bin_mod.cpp | 10 +++------- common/common.cpp | 6 ++---- common/pgm_base.cpp | 7 ++----- eeschema/widgets/symbol_preview_widget.cpp | 4 ++-- include/bin_mod.h | 3 ++- include/common.h | 3 ++- include/kiface_i.h | 2 +- include/pgm_base.h | 5 +++-- kicad/pgm_kicad.h | 2 +- pcb_calculator/pcb_calculator_frame.cpp | 6 +++--- 11 files changed, 22 insertions(+), 30 deletions(-) diff --git a/bitmap2component/bitmap2cmp_gui.cpp b/bitmap2component/bitmap2cmp_gui.cpp index e221f588c0..5aa689326a 100644 --- a/bitmap2component/bitmap2cmp_gui.cpp +++ b/bitmap2component/bitmap2cmp_gui.cpp @@ -79,7 +79,7 @@ private: wxString m_ConvertedFileName; wxSize m_frameSize; wxPoint m_framePos; - wxConfigBase* m_config; + std::unique_ptr m_config; public: BM2CMP_FRAME( KIWAY* aKiway, wxWindow* aParent ); @@ -223,8 +223,6 @@ BM2CMP_FRAME::~BM2CMP_FRAME() m_config->Write( KEYWORD_LAST_FORMAT, m_radioBoxFormat->GetSelection() ); m_config->Write( KEYWORD_LAST_MODLAYER, m_radio_PCBLayer->GetSelection() ); - delete m_config; - /* This needed for OSX: avoids further OnDraw processing after this * destructor and before the native window is destroyed */ diff --git a/common/bin_mod.cpp b/common/bin_mod.cpp index 08513995e2..1ad4b01ae0 100644 --- a/common/bin_mod.cpp +++ b/common/bin_mod.cpp @@ -29,7 +29,6 @@ BIN_MOD::BIN_MOD( const char* aName ) : m_name( aName ), - m_config( 0 ), m_history( 0 ) { } @@ -48,7 +47,7 @@ void BIN_MOD::Init() Pgm().CommonSettings()->Read( FILE_HISTORY_SIZE_KEY, &fileHistorySize, DEFAULT_FILE_HISTORY_SIZE ); m_history = new FILE_HISTORY( (unsigned) std::max( 0, fileHistorySize ), ID_FILE1 ); - m_history->Load( *m_config ); + m_history->Load( *m_config.get() ); // Prepare On Line Help. Use only lower case for help file names, in order to // avoid problems with upper/lower case file names under windows and unix. @@ -64,14 +63,11 @@ void BIN_MOD::End() { if( m_config ) { - m_history->Save( *m_config ); - + m_history->Save( *m_config.get() ); delete m_history; // Deleting a wxConfigBase writes its contents to disk if changed. - // Might be NULL if called twice, in which case nothing happens. - delete m_config; - m_config = 0; + m_config.reset(); } } diff --git a/common/common.cpp b/common/common.cpp index 9d5bca535e..d3c6bef899 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -248,15 +248,13 @@ double RoundTo0( double x, double precision ) } -wxConfigBase* GetNewConfig( const wxString& aProgName ) +std::unique_ptr GetNewConfig( const wxString& aProgName ) { - wxConfigBase* cfg = 0; wxFileName configname; configname.AssignDir( GetKicadConfigPath() ); configname.SetFullName( aProgName ); - cfg = new wxFileConfig( wxT( "" ), wxT( "" ), configname.GetFullPath() ); - return cfg; + return std::make_unique( wxT( "" ), wxT( "" ), configname.GetFullPath() ); } diff --git a/common/pgm_base.cpp b/common/pgm_base.cpp index 6235890836..5f05f13a10 100644 --- a/common/pgm_base.cpp +++ b/common/pgm_base.cpp @@ -154,7 +154,6 @@ PGM_BASE::PGM_BASE() { m_pgm_checker = NULL; m_locale = NULL; - m_common_settings = NULL; m_show_env_var_dialog = true; @@ -173,9 +172,7 @@ PGM_BASE::~PGM_BASE() void PGM_BASE::Destroy() { // unlike a normal destructor, this is designed to be called more than once safely: - - delete m_common_settings; - m_common_settings = 0; + m_common_settings.reset(); delete m_pgm_checker; m_pgm_checker = 0; @@ -562,7 +559,7 @@ void PGM_BASE::loadCommonSettings() // options only in pcbnew (which was the only canvas to support them). Since there's // no single right answer to where to pull the common settings from, we might as well // get them along with the hardware antialiasing option from pcbnew. - wxConfigBase* pcbnewConfig = GetNewConfig( wxString::FromUTF8( "pcbnew" ) ); + auto pcbnewConfig = GetNewConfig( wxString::FromUTF8( "pcbnew" ) ); wxString pcbFrameKey( PCB_EDIT_FRAME_NAME ); if( !m_common_settings->HasEntry( ICON_SCALE_KEY ) ) diff --git a/eeschema/widgets/symbol_preview_widget.cpp b/eeschema/widgets/symbol_preview_widget.cpp index 19453dcc65..e3c50c3ba6 100644 --- a/eeschema/widgets/symbol_preview_widget.cpp +++ b/eeschema/widgets/symbol_preview_widget.cpp @@ -41,8 +41,8 @@ SYMBOL_PREVIEW_WIDGET::SYMBOL_PREVIEW_WIDGET( wxWindow* aParent, KIWAY& aKiway, m_previewItem( nullptr ) { wxString eeschemaFrameKey( SCH_EDIT_FRAME_NAME ); - wxConfigBase* eeschemaConfig = GetNewConfig( Pgm().App().GetAppName() ); - m_galDisplayOptions.ReadConfig( eeschemaConfig, eeschemaFrameKey + GAL_DISPLAY_OPTIONS_KEY ); + auto eeschemaConfig = GetNewConfig( Pgm().App().GetAppName() ); + m_galDisplayOptions.ReadConfig( eeschemaConfig.get(), eeschemaFrameKey + GAL_DISPLAY_OPTIONS_KEY ); EDA_DRAW_PANEL_GAL::GAL_TYPE canvasType = aCanvasType; diff --git a/include/bin_mod.h b/include/bin_mod.h index d2ad208277..a516a76421 100644 --- a/include/bin_mod.h +++ b/include/bin_mod.h @@ -33,6 +33,7 @@ #include #include +#include class wxConfigBase; class FILE_HISTORY; @@ -55,7 +56,7 @@ struct BIN_MOD const char* m_name; ///< name of this binary module, static C string. - wxConfigBase* m_config; ///< maybe from $HOME/.${m_name} + std::unique_ptr m_config; ///< maybe from $HOME/.${m_name} FILE_HISTORY* m_history; wxString m_help_file; diff --git a/include/common.h b/include/common.h index 9c12789ed8..62410d3676 100644 --- a/include/common.h +++ b/include/common.h @@ -43,6 +43,7 @@ #include #include +#include // C++11 "polyfill" for the C++14 std::make_unique function #include "make_unique.h" @@ -310,7 +311,7 @@ const wxString PrePendPath( const wxString& aEnvVar, const wxString& aPriorityPa * @return A pointer to a new wxConfigBase derived object is returned. The caller is in charge * of deleting it. */ -wxConfigBase* GetNewConfig( const wxString& aProgName ); +std::unique_ptr GetNewConfig( const wxString& aProgName ); /** * Return the user configuration path used to store KiCad's configuration files. diff --git a/include/kiface_i.h b/include/kiface_i.h index 688d2e6af3..e72f26a234 100644 --- a/include/kiface_i.h +++ b/include/kiface_i.h @@ -100,7 +100,7 @@ public: const wxString Name() { return wxString::FromUTF8( m_bm.m_name ); } - wxConfigBase* KifaceSettings() const { return m_bm.m_config; } + wxConfigBase* KifaceSettings() const { return m_bm.m_config.get(); } /** * Function StartFlags diff --git a/include/pgm_base.h b/include/pgm_base.h index c754c8cf3b..c3f665146f 100644 --- a/include/pgm_base.h +++ b/include/pgm_base.h @@ -32,6 +32,7 @@ #define PGM_BASE_H_ #include +#include #include #include #include @@ -184,7 +185,7 @@ public: */ VTBL_ENTRY void MacOpenFile( const wxString& aFileName ) = 0; - VTBL_ENTRY wxConfigBase* CommonSettings() const { return m_common_settings; } + VTBL_ENTRY wxConfigBase* CommonSettings() const { return m_common_settings.get(); } VTBL_ENTRY void SetEditorName( const wxString& aFileName ); @@ -362,7 +363,7 @@ protected: /// Configuration settings common to all KiCad program modules, /// like as in $HOME/.kicad_common - wxConfigBase* m_common_settings; + std::unique_ptr m_common_settings; /// full path to this program wxString m_bin_dir; diff --git a/kicad/pgm_kicad.h b/kicad/pgm_kicad.h index c3eb04c74e..2a8e234dc8 100644 --- a/kicad/pgm_kicad.h +++ b/kicad/pgm_kicad.h @@ -56,7 +56,7 @@ public: FILE_HISTORY& GetFileHistory() { return *m_bm.m_history; } - wxConfigBase* PgmSettings() { return m_bm.m_config; } + wxConfigBase* PgmSettings() { return m_bm.m_config.get(); } SEARCH_STACK& SysSearch() { return m_bm.m_search; } diff --git a/pcb_calculator/pcb_calculator_frame.cpp b/pcb_calculator/pcb_calculator_frame.cpp index 04b3ad56f0..d65e6bd7dd 100644 --- a/pcb_calculator/pcb_calculator_frame.cpp +++ b/pcb_calculator/pcb_calculator_frame.cpp @@ -83,15 +83,15 @@ PCB_CALCULATOR_FRAME::PCB_CALCULATOR_FRAME( KIWAY* aKiway, wxWindow* aParent ) : m_attenuator_list.push_back( new ATTENUATOR_SPLITTER() ); m_currAttenuator = m_attenuator_list[0]; - wxConfigBase* config = GetNewConfig( Pgm().App().GetAppName() ); - LoadSettings( config ); + auto config = GetNewConfig( Pgm().App().GetAppName() ); + LoadSettings( config.get() ); ReadDataFile(); TranslineTypeSelection( m_currTransLineType ); m_TranslineSelection->SetSelection( m_currTransLineType ); - TW_Init( config ); + TW_Init( config.get() ); SetAttenuator( m_AttenuatorsSelection->GetSelection() );