From 12641ab0c0cc7e0c9cc6d7c8ea0c6c4649d52cdb Mon Sep 17 00:00:00 2001 From: orignal Date: Mon, 30 Mar 2015 10:21:52 -0400 Subject: [PATCH] fixed addressbook crash at shutdown --- AddressBook.cpp | 32 +++++++++++++++++++++++++++----- AddressBook.h | 7 +++++-- ClientContext.cpp | 5 ++--- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/AddressBook.cpp b/AddressBook.cpp index ff410ba5..ffa6883d 100644 --- a/AddressBook.cpp +++ b/AddressBook.cpp @@ -151,13 +151,29 @@ namespace client } //--------------------------------------------------------------------- - AddressBook::AddressBook (): m_IsLoaded (false), m_IsDownloading (false), + AddressBook::AddressBook (): m_Storage (nullptr), m_IsLoaded (false), m_IsDownloading (false), m_DefaultSubscription (nullptr), m_SubscriptionsUpdateTimer (nullptr) { } AddressBook::~AddressBook () { + Stop (); + } + + void AddressBook::Start () + { + StartSubscriptions (); + } + + void AddressBook::Stop () + { + StopSubscriptions (); + if (m_SubscriptionsUpdateTimer) + { + delete m_SubscriptionsUpdateTimer; + m_SubscriptionsUpdateTimer = nullptr; + } if (m_IsDownloading) { LogPrint (eLogInfo, "Subscription is downloading. Waiting for temination..."); @@ -171,18 +187,24 @@ namespace client std::this_thread::sleep_for (std::chrono::seconds (1)); // wait for 1 seconds } LogPrint (eLogError, "Subscription download hangs"); + m_IsDownloading = false; } if (m_Storage) { m_Storage->Save (m_Addresses); delete m_Storage; + m_Storage = nullptr; } - delete m_DefaultSubscription; + if (m_DefaultSubscription) + { + delete m_DefaultSubscription; + m_DefaultSubscription = nullptr; + } for (auto it: m_Subscriptions) delete it; - delete m_SubscriptionsUpdateTimer; - } - + m_Subscriptions.clear (); + } + AddressBookStorage * AddressBook::CreateStorage () { return new AddressBookFilesystemStorage (); diff --git a/AddressBook.h b/AddressBook.h index dc4ac54c..6fdae9b1 100644 --- a/AddressBook.h +++ b/AddressBook.h @@ -46,14 +46,14 @@ namespace client AddressBook (); ~AddressBook (); + void Start (); + void Stop (); bool GetIdentHash (const std::string& address, i2p::data::IdentHash& ident); bool GetAddress (const std::string& address, i2p::data::IdentityEx& identity); const i2p::data::IdentHash * FindAddress (const std::string& address); void InsertAddress (const std::string& address, const std::string& base64); // for jump service void InsertAddress (const i2p::data::IdentityEx& address); - void StartSubscriptions (); - void StopSubscriptions (); void LoadHostsFromStream (std::istream& f); void DownloadComplete (bool success); //This method returns the ".b32.i2p" address @@ -61,6 +61,9 @@ namespace client std::string ToAddress(const i2p::data::IdentityEx& ident) { return ToAddress(ident.GetIdentHash ()); } private: + void StartSubscriptions (); + void StopSubscriptions (); + AddressBookStorage * CreateStorage (); void LoadHosts (); void LoadSubscriptions (); diff --git a/ClientContext.cpp b/ClientContext.cpp index a1a1e395..4df5fe40 100644 --- a/ClientContext.cpp +++ b/ClientContext.cpp @@ -101,12 +101,11 @@ namespace client m_I2PControlService->Start (); LogPrint("I2PControl started"); } - m_AddressBook.StartSubscriptions (); + m_AddressBook.Start (); } void ClientContext::Stop () { - m_AddressBook.StopSubscriptions (); m_HttpProxy->Stop(); delete m_HttpProxy; m_HttpProxy = nullptr; @@ -148,7 +147,7 @@ namespace client m_I2PControlService = nullptr; LogPrint("I2PControl stopped"); } - + m_AddressBook.Stop (); for (auto it: m_Destinations) it.second->Stop (); m_Destinations.clear ();