diff options
author | Luo Jinghua <sunmoon1997@gmail.com> | 2011-11-19 13:10:32 +0800 |
---|---|---|
committer | Luo Jinghua <sunmoon1997@gmail.com> | 2011-11-19 13:10:32 +0800 |
commit | a7feda2da8829522c5232b4b945dcb9b1e91245c (patch) | |
tree | ed381bffd436c90e1e0561acd6b5c6dc09fb53a5 | |
parent | ec938e94e789b7f27ea103c81e5d05827cfd2d2c (diff) |
SexyAppFramework: Fixed the input driver factory leak
-rw-r--r-- | osframework/source/SexyAppFramework/InputDriverFactory.cpp | 52 | ||||
-rw-r--r-- | osframework/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: |