From 33c73d7db4597b62edb0422b702dda779b76151a Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Thu, 14 Aug 2025 11:32:18 +0100 Subject: [PATCH] Fix crashes when settings manager doesn't exist yet. --- 3d-viewer/3d_spacenav/spnav_viewer_plugin.cpp | 50 +++++++++++-------- common/bitmap_store.cpp | 13 ++--- common/dialog_shim.cpp | 50 +++++++++++-------- common/widgets/ui_common.cpp | 6 +-- common/widgets/wx_grid.cpp | 2 +- eeschema/tools/symbol_editor_control.cpp | 17 ++++--- kicad/menubar.cpp | 6 +-- pcbnew/tools/footprint_editor_control.cpp | 16 +++--- 8 files changed, 85 insertions(+), 75 deletions(-) diff --git a/3d-viewer/3d_spacenav/spnav_viewer_plugin.cpp b/3d-viewer/3d_spacenav/spnav_viewer_plugin.cpp index 41253a8e03..814cfa8202 100644 --- a/3d-viewer/3d_spacenav/spnav_viewer_plugin.cpp +++ b/3d-viewer/3d_spacenav/spnav_viewer_plugin.cpp @@ -63,33 +63,43 @@ void SPNAV_VIEWER_PLUGIN::onPollTimer( wxTimerEvent& ) void SPNAV_VIEWER_PLUGIN::OnPan( double x, double y, double z ) { - const COMMON_SETTINGS* cfg = Pgm().GetCommonSettings(); - float scale = 0.0005f * ( cfg->m_SpaceMouse.pan_speed / 5.0f ); - if( cfg->m_SpaceMouse.reverse_pan_x ) - x = -x; - if( cfg->m_SpaceMouse.reverse_pan_y ) - y = -y; - if( cfg->m_SpaceMouse.reverse_zoom ) - z = -z; - if( m_camera ) + if( const COMMON_SETTINGS* cfg = Pgm().GetCommonSettings() ) { - m_camera->Pan( SFVEC3F( x * scale, -y * scale, z * scale ) ); - m_canvas->Request_refresh(); + float scale = 0.0005f * ( cfg->m_SpaceMouse.pan_speed / 5.0f ); + + if( cfg->m_SpaceMouse.reverse_pan_x ) + x = -x; + + if( cfg->m_SpaceMouse.reverse_pan_y ) + y = -y; + + if( cfg->m_SpaceMouse.reverse_zoom ) + z = -z; + + if( m_camera ) + { + m_camera->Pan( SFVEC3F( x * scale, -y * scale, z * scale ) ); + m_canvas->Request_refresh(); + } } } void SPNAV_VIEWER_PLUGIN::OnRotate( double rx, double ry, double rz ) { - const COMMON_SETTINGS* cfg = Pgm().GetCommonSettings(); - float scale = 0.001f * ( cfg->m_SpaceMouse.rotate_speed / 5.0f ); - if( cfg->m_SpaceMouse.reverse_rotate ) - scale = -scale; - if( m_camera ) + if( const COMMON_SETTINGS* cfg = Pgm().GetCommonSettings() ) { - m_camera->RotateX( ry * scale ); - m_camera->RotateY( rx * scale ); - m_camera->RotateZ( rz * scale ); - m_canvas->Request_refresh(); + float scale = 0.001f * ( cfg->m_SpaceMouse.rotate_speed / 5.0f ); + + if( cfg->m_SpaceMouse.reverse_rotate ) + scale = -scale; + + if( m_camera ) + { + m_camera->RotateX( ry * scale ); + m_camera->RotateY( rx * scale ); + m_camera->RotateZ( rz * scale ); + m_canvas->Request_refresh(); + } } } diff --git a/common/bitmap_store.cpp b/common/bitmap_store.cpp index 861f781d59..adeca67018 100644 --- a/common/bitmap_store.cpp +++ b/common/bitmap_store.cpp @@ -207,18 +207,15 @@ wxImage BITMAP_STORE::getImage( BITMAPS aBitmapId, int aHeight ) void BITMAP_STORE::ThemeChanged() { - COMMON_SETTINGS* settings = Pgm().GetCommonSettings(); - wxString oldTheme = m_theme; + wxString oldTheme = m_theme; - if( settings ) + if( COMMON_SETTINGS* settings = Pgm().GetCommonSettings() ) { switch( settings->m_Appearance.icon_theme ) { - case ICON_THEME::LIGHT: m_theme = wxT( "light" ); break; - case ICON_THEME::DARK: m_theme = wxT( "dark" ); break; - case ICON_THEME::AUTO: - m_theme = KIPLATFORM::UI::IsDarkTheme() ? wxT( "dark" ) : wxT( "light" ); - break; + case ICON_THEME::LIGHT: m_theme = wxT( "light" ); break; + case ICON_THEME::DARK: m_theme = wxT( "dark" ); break; + case ICON_THEME::AUTO: m_theme = KIPLATFORM::UI::IsDarkTheme() ? wxT( "dark" ) : wxT( "light" ); break; } } else diff --git a/common/dialog_shim.cpp b/common/dialog_shim.cpp index 40ccd69ff2..cce870cbe4 100644 --- a/common/dialog_shim.cpp +++ b/common/dialog_shim.cpp @@ -247,21 +247,23 @@ bool DIALOG_SHIM::Show( bool show ) #endif ret = wxDialog::Show( show ); - wxRect savedDialogRect; - COMMON_SETTINGS* settings = Pgm().GetSettingsManager().GetCommonSettings(); - std::string key = m_hash_key.empty() ? GetTitle().ToStdString() : m_hash_key; + wxRect savedDialogRect; + std::string key = m_hash_key.empty() ? GetTitle().ToStdString() : m_hash_key; - auto dlgIt = settings->m_dialogControlValues.find( key ); - - if( dlgIt != settings->m_dialogControlValues.end() ) + if( COMMON_SETTINGS* settings = Pgm().GetCommonSettings() ) { - auto geoIt = dlgIt->second.find( "__geometry" ); + auto dlgIt = settings->m_dialogControlValues.find( key ); - if( geoIt != dlgIt->second.end() && geoIt->second.is_object() ) + if( dlgIt != settings->m_dialogControlValues.end() ) { - const nlohmann::json& g = geoIt->second; - savedDialogRect.SetPosition( wxPoint( g.value( "x", 0 ), g.value( "y", 0 ) ) ); - savedDialogRect.SetSize( wxSize( g.value( "w", 500 ), g.value( "h", 300 ) ) ); + auto geoIt = dlgIt->second.find( "__geometry" ); + + if( geoIt != dlgIt->second.end() && geoIt->second.is_object() ) + { + const nlohmann::json& g = geoIt->second; + savedDialogRect.SetPosition( wxPoint( g.value( "x", 0 ), g.value( "y", 0 ) ) ); + savedDialogRect.SetSize( wxSize( g.value( "w", 500 ), g.value( "h", 300 ) ) ); + } } } @@ -325,15 +327,17 @@ bool DIALOG_SHIM::Show( bool show ) void DIALOG_SHIM::resetSize() { - COMMON_SETTINGS* settings = Pgm().GetSettingsManager().GetCommonSettings(); - std::string key = m_hash_key.empty() ? GetTitle().ToStdString() : m_hash_key; + if( COMMON_SETTINGS* settings = Pgm().GetCommonSettings() ) + { + std::string key = m_hash_key.empty() ? GetTitle().ToStdString() : m_hash_key; - auto dlgIt = settings->m_dialogControlValues.find( key ); + auto dlgIt = settings->m_dialogControlValues.find( key ); - if( dlgIt == settings->m_dialogControlValues.end() ) - return; + if( dlgIt == settings->m_dialogControlValues.end() ) + return; - dlgIt->second.erase( "__geometry" ); + dlgIt->second.erase( "__geometry" ); + } } @@ -402,9 +406,13 @@ std::string DIALOG_SHIM::generateKey( const wxWindow* aWin ) const void DIALOG_SHIM::SaveControlState() { - COMMON_SETTINGS* settings = Pgm().GetSettingsManager().GetCommonSettings(); + COMMON_SETTINGS* settings = Pgm().GetCommonSettings(); + + if( !settings ) + return; + std::string dialogKey = m_hash_key.empty() ? GetTitle().ToStdString() : m_hash_key; - auto& dlgMap = settings->m_dialogControlValues[ dialogKey ]; + std::map& dlgMap = settings->m_dialogControlValues[ dialogKey ]; wxRect rect( GetPosition(), GetSize() ); nlohmann::json geom; @@ -470,7 +478,7 @@ void DIALOG_SHIM::SaveControlState() void DIALOG_SHIM::LoadControlState() { - COMMON_SETTINGS* settings = Pgm().GetSettingsManager().GetCommonSettings(); + COMMON_SETTINGS* settings = Pgm().GetCommonSettings(); if( !settings ) return; @@ -481,7 +489,7 @@ void DIALOG_SHIM::LoadControlState() if( dlgIt == settings->m_dialogControlValues.end() ) return; - const auto& dlgMap = dlgIt->second; + const std::map& dlgMap = dlgIt->second; std::function loadFn = [&]( wxWindow* win ) diff --git a/common/widgets/ui_common.cpp b/common/widgets/ui_common.cpp index 21967cfa3f..2c7407314a 100644 --- a/common/widgets/ui_common.cpp +++ b/common/widgets/ui_common.cpp @@ -112,7 +112,7 @@ wxFont getGUIFont( wxWindow* aWindow, int aRelativeSize ) font.SetPointSize( font.GetPointSize() + aRelativeSize ); - if( Pgm().GetCommonSettings()->m_Appearance.apply_icon_scale_to_fonts ) + if( Pgm().GetCommonSettings() && Pgm().GetCommonSettings()->m_Appearance.apply_icon_scale_to_fonts ) font.SetPointSize( KiROUND( KiIconScale( aWindow ) * font.GetPointSize() / 4.0 ) ); #ifdef __WXMAC__ @@ -368,14 +368,12 @@ void KIUI::Disable( wxWindow* aWindow ) void KIUI::AddBitmapToMenuItem( wxMenuItem* aMenu, const wxBitmapBundle& aImage ) { // Retrieve the global application show icon option: - bool useImagesInMenus = Pgm().GetCommonSettings()->m_Appearance.use_icons_in_menus; + bool useImagesInMenus = Pgm().GetCommonSettings() && Pgm().GetCommonSettings()->m_Appearance.use_icons_in_menus; wxItemKind menu_type = aMenu->GetKind(); if( useImagesInMenus && menu_type != wxITEM_CHECK && menu_type != wxITEM_RADIO ) - { aMenu->SetBitmap( aImage ); - } } diff --git a/common/widgets/wx_grid.cpp b/common/widgets/wx_grid.cpp index b518d0e099..671c701144 100644 --- a/common/widgets/wx_grid.cpp +++ b/common/widgets/wx_grid.cpp @@ -296,7 +296,7 @@ void WX_GRID::SetTable( wxGridTableBase* aTable, bool aTakeOwnership ) delete[] formBuilderColWidths; - EnableAlternateRowColors( Pgm().GetCommonSettings()->m_Appearance.grid_striping ); + EnableAlternateRowColors( Pgm().GetCommonSettings() && Pgm().GetCommonSettings()->m_Appearance.grid_striping ); Connect( wxEVT_GRID_COL_MOVE, wxGridEventHandler( WX_GRID::onGridColMove ), nullptr, this ); Connect( wxEVT_GRID_SELECT_CELL, wxGridEventHandler( WX_GRID::onGridCellSelect ), nullptr, this ); diff --git a/eeschema/tools/symbol_editor_control.cpp b/eeschema/tools/symbol_editor_control.cpp index c4c2797774..ea60c4f696 100644 --- a/eeschema/tools/symbol_editor_control.cpp +++ b/eeschema/tools/symbol_editor_control.cpp @@ -330,21 +330,22 @@ int SYMBOL_EDITOR_CONTROL::OpenDirectory( const TOOL_EVENT& aEvent ) wxFileName fileName( libItemName ); wxString filePath = wxEmptyString; + wxString explorerCommand; - COMMON_SETTINGS* cfg = Pgm().GetCommonSettings(); + if( COMMON_SETTINGS* cfg = Pgm().GetCommonSettings() ) + explorerCommand = cfg->m_System.file_explorer; - wxString explCommand = cfg->m_System.file_explorer; - - if( explCommand.IsEmpty() ) + if( explorerCommand.IsEmpty() ) { filePath = fileName.GetFullPath().BeforeLast( wxFileName::GetPathSeparator() ); if( !filePath.IsEmpty() && wxDirExists( filePath ) ) LaunchExternal( filePath ); + return 0; } - if( !explCommand.EndsWith( "%F" ) ) + if( !explorerCommand.EndsWith( "%F" ) ) { wxMessageBox( _( "Missing/malformed file explorer argument '%F' in common settings." ) ); return 0; @@ -355,10 +356,10 @@ int SYMBOL_EDITOR_CONTROL::OpenDirectory( const TOOL_EVENT& aEvent ) wxString fileArg = '"' + filePath + '"'; - explCommand.Replace( wxT( "%F" ), fileArg ); + explorerCommand.Replace( wxT( "%F" ), fileArg ); - if( !explCommand.IsEmpty() ) - wxExecute( explCommand ); + if( !explorerCommand.IsEmpty() ) + wxExecute( explorerCommand ); return 0; } diff --git a/kicad/menubar.cpp b/kicad/menubar.cpp index 4b9f953e29..1851b4c137 100644 --- a/kicad/menubar.cpp +++ b/kicad/menubar.cpp @@ -76,15 +76,11 @@ void KICAD_MANAGER_FRAME::doReCreateMenuBar() fileMenu->Add( KICAD_MANAGER_ACTIONS::newProject ); fileMenu->Add( KICAD_MANAGER_ACTIONS::newFromTemplate ); - if( Pgm().GetCommonSettings()->m_Git.enableGit ) - { + if( Pgm().GetCommonSettings() && Pgm().GetCommonSettings()->m_Git.enableGit ) fileMenu->Add( KICAD_MANAGER_ACTIONS::newFromRepository ); - } if( wxDir::Exists( PATHS::GetStockDemosPath() ) ) - { fileMenu->Add( KICAD_MANAGER_ACTIONS::openDemoProject ); - } fileMenu->Add( KICAD_MANAGER_ACTIONS::openProject ); diff --git a/pcbnew/tools/footprint_editor_control.cpp b/pcbnew/tools/footprint_editor_control.cpp index 781c67030a..7efff21e54 100644 --- a/pcbnew/tools/footprint_editor_control.cpp +++ b/pcbnew/tools/footprint_editor_control.cpp @@ -630,12 +630,12 @@ int FOOTPRINT_EDITOR_CONTROL::OpenDirectory( const TOOL_EVENT& aEvent ) fileExt = FILEEXT::KiCadFootprintFileExtension; wxFileName fileName( path, libItemName, fileExt ); + wxString explorerCommand; - COMMON_SETTINGS* cfg = Pgm().GetCommonSettings(); + if( COMMON_SETTINGS* cfg = Pgm().GetCommonSettings() ) + explorerCommand = cfg->m_System.file_explorer; - wxString explCommand = cfg->m_System.file_explorer; - - if( explCommand.IsEmpty() ) + if( explorerCommand.IsEmpty() ) { path = fileName.GetFullPath().BeforeLast( wxFileName::GetPathSeparator() ); @@ -645,7 +645,7 @@ int FOOTPRINT_EDITOR_CONTROL::OpenDirectory( const TOOL_EVENT& aEvent ) return 0; } - if( !explCommand.EndsWith( "%F" ) ) + if( !explorerCommand.EndsWith( "%F" ) ) { wxMessageBox( _( "Missing/malformed file explorer argument '%F' in common settings." ) ); return 0; @@ -657,10 +657,10 @@ int FOOTPRINT_EDITOR_CONTROL::OpenDirectory( const TOOL_EVENT& aEvent ) wxString fileArg = wxEmptyString; fileArg << '"' << escapedFilePath << '"'; - explCommand.Replace( wxT( "%F" ), fileArg ); + explorerCommand.Replace( wxT( "%F" ), fileArg ); - if( !explCommand.IsEmpty() ) - wxExecute( explCommand ); + if( !explorerCommand.IsEmpty() ) + wxExecute( explorerCommand ); return 0; }