From 5554f799c0914948879035f04292cc4fb35a2bf0 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Mon, 15 Apr 2019 10:37:21 +0200 Subject: [PATCH] Add some info about shared_ptr to weak_ptr future refactor --- libretroshare/src/retroshare/rsbroadcastdiscovery.h | 3 +++ libretroshare/src/retroshare/rsevents.h | 3 +++ libretroshare/src/services/broadcastdiscoveryservice.cc | 9 +++++++-- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/libretroshare/src/retroshare/rsbroadcastdiscovery.h b/libretroshare/src/retroshare/rsbroadcastdiscovery.h index f0a2ee037..ff487c52e 100644 --- a/libretroshare/src/retroshare/rsbroadcastdiscovery.h +++ b/libretroshare/src/retroshare/rsbroadcastdiscovery.h @@ -37,6 +37,9 @@ class RsBroadcastDiscovery; /** * Pointer to global instance of RsBroadcastDiscovery service implementation * @jsonapi{development} + * + * TODO: this should become std::weak_ptr once we have a reasonable services + * management. */ extern std::shared_ptr rsBroadcastDiscovery; diff --git a/libretroshare/src/retroshare/rsevents.h b/libretroshare/src/retroshare/rsevents.h index 9d8a0392a..d441936a6 100644 --- a/libretroshare/src/retroshare/rsevents.h +++ b/libretroshare/src/retroshare/rsevents.h @@ -33,6 +33,9 @@ class RsEvents; /** * Pointer to global instance of RsEvents service implementation * @jsonapi{development} + * + * TODO: this should become std::weak_ptr once we have a reasonable services + * management. */ extern std::shared_ptr rsEvents; diff --git a/libretroshare/src/services/broadcastdiscoveryservice.cc b/libretroshare/src/services/broadcastdiscoveryservice.cc index 19f612414..39fbc13f0 100644 --- a/libretroshare/src/services/broadcastdiscoveryservice.cc +++ b/libretroshare/src/services/broadcastdiscoveryservice.cc @@ -170,10 +170,15 @@ void BroadcastDiscoveryService::data_tick() rbdr.locator.port() ); mRsPeers.connectAttempt(rbdr.mSslId); } - else if(rsEvents) + else { typedef RsBroadcastDiscoveryPeerFoundEvent Evt_t; - rsEvents->postEvent(std::unique_ptr(new Evt_t(rbdr))); + + // Ensure rsEvents is not deleted while we use it + std::shared_ptr lockedRsEvents = rsEvents; + if(lockedRsEvents) + lockedRsEvents->postEvent( + std::unique_ptr(new Evt_t(rbdr)) ); } } }