From 07a5d8970562064be0e3f4723803fb680f5b8faa Mon Sep 17 00:00:00 2001 From: Luo Jinghua Date: Sat, 19 Nov 2011 13:11:03 +0800 Subject: SexyAppFramework: Fixed the sound driver factory leak --- .../source/SexyAppFramework/SoundDriverFactory.cpp | 57 +++++++++++++++++++--- .../source/SexyAppFramework/SoundDriverFactory.h | 8 ++- 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/osframework/source/SexyAppFramework/SoundDriverFactory.cpp b/osframework/source/SexyAppFramework/SoundDriverFactory.cpp index 88888ab..438fc5a 100644 --- a/osframework/source/SexyAppFramework/SoundDriverFactory.cpp +++ b/osframework/source/SexyAppFramework/SoundDriverFactory.cpp @@ -26,13 +26,55 @@ SoundDriverFactory::~SoundDriverFactory () { } -SoundDriverFactory* SoundDriverFactory::GetSoundDriverFactory () +namespace Sexy { + +class StaticSoundDriverFactory { - static SoundDriverFactory * theSoundDriverFactory; +public: + struct StaticData { + SoundDriverFactory* mFactory; + bool mDone; + }; + + StaticSoundDriverFactory(StaticData* data) + { + mData = data; + } + + SoundDriverFactory* Get(StaticData* data) + { + if (data->mDone) + return 0; + + if (data->mFactory) + return data->mFactory; + + data->mFactory = new SoundDriverFactory; + return data->mFactory; + } + + ~StaticSoundDriverFactory() + { + if (!mData) + return; + + mData->mDone = true; + if (mData->mFactory) + delete mData->mFactory; + } + +private: + StaticData* mData; +}; + +static StaticSoundDriverFactory::StaticData aData; +static StaticSoundDriverFactory soundDriverFactory(&aData); - if (!theSoundDriverFactory) - theSoundDriverFactory = new SoundDriverFactory (); - return theSoundDriverFactory; +} + +SoundDriverFactory* SoundDriverFactory::GetSoundDriverFactory () +{ + return soundDriverFactory.Get(&aData); } /* This is a hack that preventing gcc from striping drivers out of @@ -52,11 +94,12 @@ SoundDriverGetter SoundDriverGetters []= { GetAudiereSoundDriver, #endif #ifdef SEXY_DIRECT_SOUND_DRIVER - GetDSoundDriver, + GetDSoundDriver, #endif #ifdef SEXY_OPENAL_SOUND_DRIVER - GetOpenALSoundDriver, + GetOpenALSoundDriver, #endif + GetDummySoundDriver, NULL }; diff --git a/osframework/source/SexyAppFramework/SoundDriverFactory.h b/osframework/source/SexyAppFramework/SoundDriverFactory.h index 86331f8..63ec872 100644 --- a/osframework/source/SexyAppFramework/SoundDriverFactory.h +++ b/osframework/source/SexyAppFramework/SoundDriverFactory.h @@ -30,6 +30,8 @@ class SoundDriverFactory: public DriverFactory void Load(); private: + friend class StaticSoundDriverFactory; + SoundDriverFactory (); ~SoundDriverFactory (); }; @@ -43,7 +45,8 @@ class SoundDriverRegistor SoundDriverFactory* factory; factory = SoundDriverFactory::GetSoundDriverFactory (); - factory->AddDriver (mDriver); + if (factory) + factory->AddDriver (mDriver); } ~SoundDriverRegistor() @@ -51,7 +54,8 @@ class SoundDriverRegistor SoundDriverFactory* factory; factory = SoundDriverFactory::GetSoundDriverFactory (); - factory->RemoveDriver (mDriver); + if (factory) + factory->RemoveDriver (mDriver); } private: -- cgit v1.2.3