From a7feda2da8829522c5232b4b945dcb9b1e91245c Mon Sep 17 00:00:00 2001 From: Luo Jinghua Date: Sat, 19 Nov 2011 13:10:32 +0800 Subject: SexyAppFramework: Fixed the input driver factory leak --- .../source/SexyAppFramework/InputDriverFactory.cpp | 52 +++++++++++++++++++--- .../source/SexyAppFramework/InputDriverFactory.h | 10 +++-- 2 files changed, 54 insertions(+), 8 deletions(-) diff --git a/osframework/source/SexyAppFramework/InputDriverFactory.cpp b/osframework/source/SexyAppFramework/InputDriverFactory.cpp index 8be3c15..fad538f 100644 --- a/osframework/source/SexyAppFramework/InputDriverFactory.cpp +++ b/osframework/source/SexyAppFramework/InputDriverFactory.cpp @@ -21,13 +21,55 @@ InputDriverFactory::~InputDriverFactory () { } -InputDriverFactory* InputDriverFactory::GetInputDriverFactory () +namespace Sexy { + +class StaticInputDriverFactory { - static InputDriverFactory * theInputDriverFactory; +public: + struct StaticData { + InputDriverFactory* mFactory; + bool mDone; + }; + + StaticInputDriverFactory(StaticData* data) + { + mData = data; + } + + InputDriverFactory* Get(StaticData* data) + { + if (data->mDone) + return 0; + + if (data->mFactory) + return data->mFactory; + + data->mFactory = new InputDriverFactory; + return data->mFactory; + } + + ~StaticInputDriverFactory() + { + if (!mData) + return; + + mData->mDone = true; + if (mData->mFactory) + delete mData->mFactory; + } - if (!theInputDriverFactory) - theInputDriverFactory = new InputDriverFactory (); - return theInputDriverFactory; +private: + StaticData* mData; +}; + +static StaticInputDriverFactory::StaticData aData; +static StaticInputDriverFactory inputDriverFactory(&aData); + +} + +InputDriverFactory* InputDriverFactory::GetInputDriverFactory () +{ + return inputDriverFactory.Get(&aData); } /* This is a hack that preventing gcc from striping drivers out of diff --git a/osframework/source/SexyAppFramework/InputDriverFactory.h b/osframework/source/SexyAppFramework/InputDriverFactory.h index fbe5158..6808a2d 100644 --- a/osframework/source/SexyAppFramework/InputDriverFactory.h +++ b/osframework/source/SexyAppFramework/InputDriverFactory.h @@ -29,9 +29,11 @@ class InputDriverFactory: public DriverFactory static InputDriverFactory* GetInputDriverFactory (); private: - void Load(); + void Load(); private: + friend class StaticInputDriverFactory; + InputDriverFactory (); ~InputDriverFactory (); }; @@ -45,7 +47,8 @@ class InputDriverRegistor InputDriverFactory* factory; factory = InputDriverFactory::GetInputDriverFactory (); - factory->AddDriver (mDriver); + if (factory) + factory->AddDriver (mDriver); } ~InputDriverRegistor() @@ -53,7 +56,8 @@ class InputDriverRegistor InputDriverFactory* factory; factory = InputDriverFactory::GetInputDriverFactory (); - factory->RemoveDriver (mDriver); + if (factory) + factory->RemoveDriver (mDriver); } private: -- cgit v1.2.3