From 2849183f1f54b33ae5dc9fdb2eebb61d83f84bf2 Mon Sep 17 00:00:00 2001 From: Florian Egger Date: Thu, 25 Jun 2026 11:33:29 +0200 Subject: [PATCH] Switch HashTable to shared_ptr and add missing returns Use std::shared_ptr in HashTable to avoid raw pointer ownership issues. Also add missing return statements in Database ID lookup helpers. --- include/Containers.h | 6 +++--- src/Database.cpp | 4 +++- src/HashTable.cpp | 6 +++--- src/PlayerLocal.cpp | 6 +++--- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/include/Containers.h b/include/Containers.h index b273cb7..e6c429a 100644 --- a/include/Containers.h +++ b/include/Containers.h @@ -53,8 +53,8 @@ class HashTable { HashTable ( HashTable && ) noexcept = default; HashTable &operator= ( HashTable && ) noexcept = default; - void ht_insert ( const std::string &key, Track* t ); - Track* ht_lookup ( const std::string &key ) const; + void ht_insert ( const std::string &key, std::shared_ptr t ); + std::shared_ptr ht_lookup ( const std::string &key ) const; bool ht_delete ( const std::string &key ); std::vector listAll () const; @@ -72,7 +72,7 @@ class HashTable { struct Entry { std::string key; uint64_t hash = 0; - Track* track; + std::shared_ptr track; }; // helpers diff --git a/src/Database.cpp b/src/Database.cpp index ac9c321..af8efad 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -252,7 +252,7 @@ std::optional db::getAlbumId( const std::string& albumTitle ) { if (sqlite3_step( stmt ) == SQLITE_ROW ) { id = sqlite3_column_int( stmt, 0 ); } - + return id; } std::optional db::getArtistId( const std::string& artistName ) { @@ -266,6 +266,7 @@ std::optional db::getArtistId( const std::string& artistName ) { if (sqlite3_step( stmt ) == SQLITE_ROW ) { id = sqlite3_column_int( stmt, 0 ); } + return id; } std::optional db::getGenreId( const std::string& genre ) { @@ -279,4 +280,5 @@ std::optional db::getGenreId( const std::string& genre ) { if (sqlite3_step( stmt ) == SQLITE_ROW ) { id = sqlite3_column_int( stmt, 0 ); } + return id; } diff --git a/src/HashTable.cpp b/src/HashTable.cpp index 63471c0..0f209a7 100644 --- a/src/HashTable.cpp +++ b/src/HashTable.cpp @@ -108,7 +108,7 @@ std::ptrdiff_t ht::probe ( const std::string &key, uint64_t hash, bool insert ) #endif } // probe -void ht::ht_insert( const std::string& key, Track* t ) { +void ht::ht_insert( const std::string& key, std::shared_ptr t ) { //double loadFactor = static_cast ( size_ ) / static_cast ( ctrl_.size()); if ( loadFactor_ > MAX_LOAD ) rehash( ctrl_.size() * 2 ); @@ -121,7 +121,7 @@ void ht::ht_insert( const std::string& key, Track* t ) { tmp.key = key; tmp.hash = hash; tmp.track = t; - entries_.at( idx ) = std::move( tmp ); + entries_.at( idx ) = std::move(tmp); ++size_; } else { entries_.at( idx ).track = t; @@ -129,7 +129,7 @@ void ht::ht_insert( const std::string& key, Track* t ) { ctrl_.at( idx ) = fingerprint( hash ); } -Track* ht::ht_lookup( const std::string& key ) const { +std::shared_ptr ht::ht_lookup( const std::string& key ) const { uint64_t hash = hashString( key ); auto idx = probe( key, hash, false ); if ( idx != -1 ) return entries_.at( static_cast ( idx )).track; diff --git a/src/PlayerLocal.cpp b/src/PlayerLocal.cpp index e96b7ba..3e819ee 100644 --- a/src/PlayerLocal.cpp +++ b/src/PlayerLocal.cpp @@ -61,8 +61,8 @@ void PlayerLocal::scanLibrary(){ void PlayerLocal::loadLibrary() { db_.fetchAll( library ); for ( auto t : library ) { - tracks_.ht_insert(t.title.toCString(), &t ); - artists_.ht_insert( t.artist.toCString(), &t ); - albums_.ht_insert( t.album.toCString(), &t ); + tracks_.ht_insert(t.title.toCString(), std::make_shared (t) ); + artists_.ht_insert( t.artist.toCString(), std::make_shared (t) ); + albums_.ht_insert( t.album.toCString(), std::make_shared (t) ); } }