Reduce scope of Application object lifetime

This commit is contained in:
Vinnie Falco
2013-09-12 10:06:08 -07:00
parent 6a84f73d2a
commit ec3c4ff77a
3 changed files with 38 additions and 7 deletions

View File

@@ -18,7 +18,16 @@ class ApplicationImp
, LeakChecked <ApplicationImp>
, 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 <size_t> ();
@@ -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 <Application> 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 <ApplicationImp>::getInstance (lifetime);
}
#endif
//------------------------------------------------------------------------------

View File

@@ -70,6 +70,8 @@ private:
SharedState m_sharedState;
public:
static Application* New ();
virtual ~Application () { }
virtual boost::asio::io_service& getIOService () = 0;

View File

@@ -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 <Application> 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 <Application> app (Application::New ());
setupServer ();
setCallingThreadName ("io");
startServer ();