From 520173ef5dcdbf8ed79721fa89973bc37b7cf693 Mon Sep 17 00:00:00 2001 From: Crash Cringle <30600688+CrashCringle12@users.noreply.github.com> Date: Fri, 4 Oct 2024 10:30:17 -0400 Subject: [PATCH] Safe delete Groups --- src/Group.cpp | 4 ++++ src/SongManager.cpp | 36 ++++++++++++++++++++++-------------- src/SongManager.h | 8 +++++--- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/Group.cpp b/src/Group.cpp index eab693e3bc..afb6f77565 100644 --- a/src/Group.cpp +++ b/src/Group.cpp @@ -29,6 +29,10 @@ Group::Group() { m_sAuthorsNotes = ""; } +Group::~Group() { + SONGMAN->GetGroupGroupMap().clear(); + m_sCredits.clear(); +} Group::Group(const RString &sPath) { RString sGroupIniPath = sPath + "/Group.ini"; RString credits = ""; diff --git a/src/SongManager.cpp b/src/SongManager.cpp index f4932f9576..f499b26480 100644 --- a/src/SongManager.cpp +++ b/src/SongManager.cpp @@ -269,7 +269,7 @@ void SongManager::SanityCheckGroupDir( RString sDir ) const } } -void SongManager::AddGroup( RString sDir, RString sGroupDirName, Group group ) +void SongManager::AddGroup( RString sDir, RString sGroupDirName, Group* group ) { unsigned j; for(j = 0; j < m_sSongGroupNames.size(); ++j) @@ -285,9 +285,9 @@ void SongManager::AddGroup( RString sDir, RString sGroupDirName, Group group ) std::vector arrayGroupBanners; // First check if there is a banner provided in group.ini - if( group.GetBannerPath() != "" ) + if( group->GetBannerPath() != "" ) { - GetDirListing( sDir+sGroupDirName+"/"+group.GetBannerPath(), arrayGroupBanners ); + GetDirListing( sDir+sGroupDirName+"/"+group->GetBannerPath(), arrayGroupBanners ); } GetDirListing( sDir+sGroupDirName+"/*.png", arrayGroupBanners ); GetDirListing( sDir+sGroupDirName+"/*.jpg", arrayGroupBanners ); @@ -339,18 +339,20 @@ void SongManager::AddGroup( RString sDir, RString sGroupDirName, Group group ) } */ m_sSongGroupNames.push_back( sGroupDirName ); - + // add to the group list + m_pGroups.push_back( group ); + if (m_mapGroupsByName.find(sGroupDirName) == m_mapGroupsByName.end()) { m_mapGroupsByName[sGroupDirName] = group; } // Add the group to its series if the group has one and if the series exists - if( group.GetSeries() != "" ) + if( group->GetSeries() != "" ) { - std::vector& series = m_mapSeries[group.GetSeries()]; - if( std::find(series.begin(), series.end(), &group) == series.end() ) - series.push_back(&group); + std::vector& series = m_mapSeries[group->GetSeries()]; + if( std::find(series.begin(), series.end(), group) == series.end() ) + series.push_back(group); } //m_sSongGroupBackgroundPaths.push_back( sBackgroundPath ); } @@ -433,7 +435,7 @@ void SongManager::LoadSongDir( RString sDir, LoadingWindow *ld, bool onlyAdditio SongPointerVector& index_entry = m_mapSongGroupIndex[sGroupDirName]; RString group_base_name= Basename(sGroupDirName); - Group group = Group(sDir + sGroupDirName); + Group* group = new Group(sDir + sGroupDirName); for( unsigned j=0; j< arraySongDirs.size(); ++j ) // for each song dir { @@ -469,10 +471,10 @@ void SongManager::LoadSongDir( RString sDir, LoadingWindow *ld, bool onlyAdditio continue; } // Apply Group Offset if applicable - if( group.GetSyncOffset() != 0 ) + if( group->GetSyncOffset() != 0 ) { - LOG->Trace("Applying group offset of %i ms to \"%s\"", group.GetSyncOffset(), pNewSong->GetSongDir().c_str() ); - pNewSong->m_SongTiming.m_fBeat0GroupOffsetInSeconds = group.GetSyncOffset(); + LOG->Trace("Applying group offset of %i ms to \"%s\"", group->GetSyncOffset(), pNewSong->GetSongDir().c_str() ); + pNewSong->m_SongTiming.m_fBeat0GroupOffsetInSeconds = group->GetSyncOffset(); const std::vector& vpSteps = pNewSong->GetAllSteps(); for (Steps* s : vpSteps) { @@ -480,7 +482,7 @@ void SongManager::LoadSongDir( RString sDir, LoadingWindow *ld, bool onlyAdditio // from the song and is already changed. if( s->m_Timing.empty() ) continue; - s->m_Timing.m_fBeat0GroupOffsetInSeconds = group.GetSyncOffset(); + s->m_Timing.m_fBeat0GroupOffsetInSeconds = group->GetSyncOffset(); } } @@ -594,7 +596,13 @@ void SongManager::FreeSongs() { RageUtil::SafeDelete( song ); } + // Loop through all groups and delete them. + for (Group *group : m_pGroups) { + RageUtil::SafeDelete(group); + } + m_pSongs.clear(); + m_pGroups.clear(); m_SongsByDir.clear(); // also free the songs that have been deleted from disk @@ -899,7 +907,7 @@ Group* SongManager::GetGroupFromName( const RString& sGroupName ) const { auto iter = m_mapGroupsByName.find( sGroupName ); if( iter != m_mapGroupsByName.end() ) - return const_cast(&iter->second); + return iter->second; return nullptr; } diff --git a/src/SongManager.h b/src/SongManager.h index 64c405c68b..b114e86f2d 100644 --- a/src/SongManager.h +++ b/src/SongManager.h @@ -146,7 +146,7 @@ public: void GetPreferredSortSongs( std::vector &AddTo ) const; std::map> GetPreferredSortSongsMap() const { return m_mapPreferredSectionToSongs;}; RString SongToPreferredSortSectionName( const Song *pSong ) const; - std::map GetGroupGroupMap() const { return m_mapGroupsByName;}; + std::map GetGroupGroupMap() const { return m_mapGroupsByName;}; std::map> GetSeriesGroupMap() const { return m_mapSeries;}; Group* GetGroupFromName( const RString &sGroupName ) const; std::vector GetPreferredSortSectionNames() const; @@ -215,12 +215,14 @@ protected: void LoadSongDir( RString sDir, LoadingWindow *ld, bool onlyAdditions ); bool GetExtraStageInfoFromCourse( bool bExtra2, RString sPreferredGroup, Song*& pSongOut, Steps*& pStepsOut, StepsType stype ); void SanityCheckGroupDir( RString sDir ) const; - void AddGroup( RString sDir, RString sGroupDirName, Group group ); + void AddGroup( RString sDir, RString sGroupDirName, Group* group ); int GetNumEditsLoadedFromProfile( ProfileSlot slot ) const; void AddSongToList(Song* new_song); /** @brief All of the songs that can be played. */ std::vector m_pSongs; + /** @brief All of the groups available */ + std::vector m_pGroups; std::map m_SongsByDir; std::set m_GroupsToNeverCache; @@ -246,7 +248,7 @@ protected: std::vector m_sSongGroupNames; //vector m_sSongGroupBackgroundPaths; // each song group may have a background associated with it (very rarely) - std::map m_mapGroupsByName; + std::map m_mapGroupsByName; std::map> m_mapSeries; struct Comp { bool operator()(const RString& s, const RString &t) const { return CompareRStringsAsc(s,t); } };