From dbe8f7f49965b05a2b17b61f5bdc428b9e043058 Mon Sep 17 00:00:00 2001 From: Teo Mrnjavac Date: Tue, 16 Apr 2013 07:14:34 +0200 Subject: [PATCH] Make the PeerInfo cache contain weak pointers. --- src/libtomahawk/Typedefs.h | 1 + src/libtomahawk/sip/PeerInfo.cpp | 16 +++++++++++----- src/libtomahawk/sip/PeerInfo.h | 2 +- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/libtomahawk/Typedefs.h b/src/libtomahawk/Typedefs.h index fb337c948..9ed6d30c7 100644 --- a/src/libtomahawk/Typedefs.h +++ b/src/libtomahawk/Typedefs.h @@ -61,6 +61,7 @@ namespace Tomahawk typedef QSharedPointer album_ptr; typedef QWeakPointer album_wptr; typedef QSharedPointer peerinfo_ptr; + typedef QWeakPointer peerinfo_wptr; typedef QSharedPointer dyncontrol_ptr; typedef QSharedPointer geninterface_ptr; diff --git a/src/libtomahawk/sip/PeerInfo.cpp b/src/libtomahawk/sip/PeerInfo.cpp index 356e16a64..786e11315 100644 --- a/src/libtomahawk/sip/PeerInfo.cpp +++ b/src/libtomahawk/sip/PeerInfo.cpp @@ -30,7 +30,7 @@ namespace Tomahawk { -QHash< QString, peerinfo_ptr > PeerInfo::s_peersByCacheKey = QHash< QString, peerinfo_ptr >(); +QHash< QString, peerinfo_wptr > PeerInfo::s_peersByCacheKey = QHash< QString, peerinfo_wptr >(); QHash< SipPlugin*, peerinfo_ptr > PeerInfo::s_selfPeersBySipPlugin = QHash< SipPlugin*, peerinfo_ptr >(); @@ -76,9 +76,9 @@ Tomahawk::peerinfo_ptr PeerInfo::get( SipPlugin* parent, const QString& id, GetOptions options ) { const QString key = peerCacheKey( parent, id ); - if ( s_peersByCacheKey.contains( key ) ) + if ( s_peersByCacheKey.contains( key ) && !s_peersByCacheKey.value( key ).isNull() ) { - return s_peersByCacheKey.value( key ); + return s_peersByCacheKey.value( key ).toStrongRef(); } // if AutoCreate isn't enabled nothing to do here @@ -89,7 +89,7 @@ PeerInfo::get( SipPlugin* parent, const QString& id, GetOptions options ) peerinfo_ptr peerInfo( new PeerInfo( parent, id ) ); peerInfo->setWeakRef( peerInfo.toWeakRef() ); - s_peersByCacheKey.insert( key, peerInfo ); + s_peersByCacheKey.insert( key, peerInfo.toWeakRef() ); return peerInfo; } @@ -98,7 +98,13 @@ PeerInfo::get( SipPlugin* parent, const QString& id, GetOptions options ) QList< Tomahawk::peerinfo_ptr > PeerInfo::getAll() { - return s_peersByCacheKey.values(); + QList< Tomahawk::peerinfo_ptr > strongRefs; + foreach ( Tomahawk::peerinfo_wptr wptr, s_peersByCacheKey.values() ) + { + if ( !wptr.isNull() ) + strongRefs << wptr.toStrongRef(); + } + return strongRefs; } diff --git a/src/libtomahawk/sip/PeerInfo.h b/src/libtomahawk/sip/PeerInfo.h index eae1637f5..2b49548b3 100644 --- a/src/libtomahawk/sip/PeerInfo.h +++ b/src/libtomahawk/sip/PeerInfo.h @@ -119,7 +119,7 @@ private: PeerInfo( SipPlugin* parent, const QString& id ); void announce(); - static QHash< QString, peerinfo_ptr > s_peersByCacheKey; + static QHash< QString, peerinfo_wptr > s_peersByCacheKey; static QHash< SipPlugin*, peerinfo_ptr > s_selfPeersBySipPlugin; QWeakPointer< Tomahawk::PeerInfo > m_ownRef;