From ec3c4ff77a43c7f68034abc9c2afb46dbc79e220 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Thu, 12 Sep 2013 10:06:08 -0700 Subject: [PATCH] Reduce scope of Application object lifetime --- src/ripple_app/main/ripple_Application.cpp | 31 +++++++++++++++++++++- src/ripple_app/main/ripple_Application.h | 2 ++ src/ripple_app/main/ripple_RippleMain.cpp | 12 ++++----- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/ripple_app/main/ripple_Application.cpp b/src/ripple_app/main/ripple_Application.cpp index ed553d2b0..80a6e7f65 100644 --- a/src/ripple_app/main/ripple_Application.cpp +++ b/src/ripple_app/main/ripple_Application.cpp @@ -18,7 +18,16 @@ class ApplicationImp , LeakChecked , PeerFinder::Callback { +private: + static ApplicationImp* s_instance; + public: + static Application& getInstance () + { + bassert (s_instance != nullptr); + return *s_instance; + } + // RAII container for a boost::asio::io_service run by beast threads class IoServiceThread { @@ -167,6 +176,9 @@ public: , mSweepTimer (m_auxService) , mShutdown (false) { + bassert (s_instance == nullptr); + s_instance = this; + // VFALCO TODO remove these once the call is thread safe. HashMaps::getInstance ().initializeNonce (); @@ -196,6 +208,9 @@ public: delete mWalletDB; mWalletDB = nullptr; } + + bassert (s_instance == this); + s_instance = nullptr; } //-------------------------------------------------------------------------- @@ -1192,8 +1207,22 @@ void ApplicationImp::onAnnounceAddress () //------------------------------------------------------------------------------ +ApplicationImp* ApplicationImp::s_instance; + +//------------------------------------------------------------------------------ + +Application* Application::New () +{ + ScopedPointer object (new ApplicationImp); + return object.release(); +} + Application& getApp () { + return ApplicationImp::getInstance (); +} + +#if 0 #if RIPPLE_APPLICATION_CLEAN_EXIT // Application object will be deleted on exit. If the code doesn't exit // cleanly this could cause hangs or crashes on exit. @@ -1208,7 +1237,7 @@ Application& getApp () #endif return *SharedSingleton ::getInstance (lifetime); -} +#endif //------------------------------------------------------------------------------ diff --git a/src/ripple_app/main/ripple_Application.h b/src/ripple_app/main/ripple_Application.h index bba0e3614..06a8fb54e 100644 --- a/src/ripple_app/main/ripple_Application.h +++ b/src/ripple_app/main/ripple_Application.h @@ -70,6 +70,8 @@ private: SharedState m_sharedState; public: + static Application* New (); + virtual ~Application () { } virtual boost::asio::io_service& getIOService () = 0; diff --git a/src/ripple_app/main/ripple_RippleMain.cpp b/src/ripple_app/main/ripple_RippleMain.cpp index 50cf90b79..67fc5bc59 100644 --- a/src/ripple_app/main/ripple_RippleMain.cpp +++ b/src/ripple_app/main/ripple_RippleMain.cpp @@ -128,15 +128,13 @@ public: explicit RippleUnitTests (bool shouldLog) : m_shouldLog (shouldLog) { + setupConfigForUnitTests (&getConfig ()); + // VFALCO NOTE It sucks that we have to do this but some // code demands the Application object exists. // - // TODO To find out who, just change the #if 1 to #if 0 -#if 1 - setupConfigForUnitTests (&getConfig ()); - - getApp (); -#endif + // TODO To find out who, just comment the next line out + m_app = Application::New (); setAssertOnFailure (false); } @@ -172,6 +170,7 @@ private: private: bool const m_shouldLog; + ScopedPointer m_app; }; static int runUnitTests (String const& match, String const& format) @@ -455,6 +454,7 @@ int RippleMain::run (int argc, char const* const* argv) if (!vm.count ("parameters")) { // No arguments. Run server. + ScopedPointer app (Application::New ()); setupServer (); setCallingThreadName ("io"); startServer ();