Don't sort on lost references

The arraystring reference is lost when we don't keep the wxFileName
variable in scope.

This also adds removing invalid paths and checking for correct version
before sorting

Fixes https://gitlab.com/kicad/code/kicad/-/issues/17749
This commit is contained in:
Seth Hillbrand 2024-04-17 10:10:34 -07:00
parent d79619edd1
commit d2701323a8
1 changed files with 35 additions and 7 deletions

View File

@ -731,19 +731,47 @@ bool SETTINGS_MANAGER::GetPreviousVersionPaths( std::vector<wxString>* aPaths )
}
}
alg::delete_if( *aPaths, []( const wxString& aPath ) -> bool
{
wxFileName fulldir = wxFileName::DirName( aPath );
const wxArrayString& dirs = fulldir.GetDirs();
if( dirs.empty() || !fulldir.IsDirReadable() )
return true;
std::string ver = dirs.back().ToStdString();
if( !extractVersion( ver ) )
return true;
return false;
} );
std::sort( aPaths->begin(), aPaths->end(),
[&]( const wxString& a, const wxString& b ) -> bool
{
wxString verA = wxFileName::DirName( a ).GetDirs().back();
wxString verB = wxFileName::DirName( b ).GetDirs().back();
wxFileName aPath = wxFileName::DirName( a );
wxFileName bPath = wxFileName::DirName( b );
if( !extractVersion( verA.ToStdString() )
|| !extractVersion( verB.ToStdString() ) )
{
const wxArrayString& aDirs = aPath.GetDirs();
const wxArrayString& bDirs = bPath.GetDirs();
if( aDirs.empty() )
return false;
}
return compareVersions( verA.ToStdString(), verB.ToStdString() ) >= 0;
if( bDirs.empty() )
return true;
std::string verA = aDirs.back().ToStdString();
std::string verB = bDirs.back().ToStdString();
if( !extractVersion( verA ) )
return false;
if( !extractVersion( verB ) )
return true;
return compareVersions( verA, verB ) >= 0;
} );
return aPaths->size() > 0;