diff options
author | Luo Jinghua <sunmoon1997@gmail.com> | 2011-11-19 13:11:03 +0800 |
---|---|---|
committer | Luo Jinghua <sunmoon1997@gmail.com> | 2011-11-19 13:11:03 +0800 |
commit | 07a5d8970562064be0e3f4723803fb680f5b8faa (patch) | |
tree | 09582997d139db22de3d7b6540ae627b7f753c81 | |
parent | a7feda2da8829522c5232b4b945dcb9b1e91245c (diff) |
SexyAppFramework: Fixed the sound driver factory leak
-rw-r--r-- | osframework/source/SexyAppFramework/SoundDriverFactory.cpp | 57 | ||||
-rw-r--r-- | osframework/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: |