From d62372bb23dd5c054ac0a33bb4d8aed22db622d6 Mon Sep 17 00:00:00 2001 From: Luo Jinghua Date: Sat, 19 Nov 2011 13:11:34 +0800 Subject: SexyAppFramework: Fixed the video driver factory leak --- .../source/SexyAppFramework/VideoDriverFactory.cpp | 62 ++++++++++++++++++---- .../source/SexyAppFramework/VideoDriverFactory.h | 8 ++- 2 files changed, 59 insertions(+), 11 deletions(-) diff --git a/osframework/source/SexyAppFramework/VideoDriverFactory.cpp b/osframework/source/SexyAppFramework/VideoDriverFactory.cpp index 9a2d49e..e843361 100644 --- a/osframework/source/SexyAppFramework/VideoDriverFactory.cpp +++ b/osframework/source/SexyAppFramework/VideoDriverFactory.cpp @@ -21,16 +21,58 @@ VideoDriverFactory::~VideoDriverFactory () { } -VideoDriverFactory* VideoDriverFactory::GetVideoDriverFactory () +namespace Sexy { + +class StaticVideoDriverFactory { - static VideoDriverFactory * theVideoDriverFactory; +public: + struct StaticData { + VideoDriverFactory* mFactory; + bool mDone; + }; + + StaticVideoDriverFactory(StaticData* data) + { + mData = data; + } + + VideoDriverFactory* Get(StaticData* data) + { + if (data->mDone) + return 0; + + if (data->mFactory) + return data->mFactory; + + data->mFactory = new VideoDriverFactory; + return data->mFactory; + } + + ~StaticVideoDriverFactory() + { + if (!mData) + return; + + mData->mDone = true; + if (mData->mFactory) + delete mData->mFactory; + } + +private: + StaticData* mData; +}; + +static StaticVideoDriverFactory::StaticData aData; +static StaticVideoDriverFactory videoDriverFactory(&aData); - if (!theVideoDriverFactory) - theVideoDriverFactory = new VideoDriverFactory (); - return theVideoDriverFactory; } -/* This is a hack that preventing gcc from striping drivers out of +VideoDriverFactory* VideoDriverFactory::GetVideoDriverFactory () +{ + return videoDriverFactory.Get(&aData); +} + +/* This is a hack that preventing linker from striping drivers out of * binary. */ extern VideoDriver* GetAGLVideoDriver(); @@ -44,10 +86,10 @@ extern VideoDriver* GetAndroidVideoDriver(); typedef VideoDriver* (* VideoDriverGetter)(); VideoDriverGetter VideoDriverGetters []= { #ifdef SEXY_AGL_DRIVER - GetAGLVideoDriver, + GetAGLVideoDriver, #endif #ifdef SEXY_EAGL_DRIVER - GetEAGLVideoDriver, + GetEAGLVideoDriver, #endif #ifdef SEXY_GLX_DRIVER GetGLXVideoDriver, @@ -61,9 +103,11 @@ VideoDriverGetter VideoDriverGetters []= { #ifdef SEXY_CEGLES_DRIVER GetCEGLESVideoDriver, #endif + #ifdef SEXY_XGLES_DRIVER - GetXGLESVideoDriver, + GetXGLESVideoDriver, #endif + #ifdef SEXY_ANDROIDGLES_DRIVER GetAndroidVideoDriver, #endif diff --git a/osframework/source/SexyAppFramework/VideoDriverFactory.h b/osframework/source/SexyAppFramework/VideoDriverFactory.h index 65641eb..ee17654 100644 --- a/osframework/source/SexyAppFramework/VideoDriverFactory.h +++ b/osframework/source/SexyAppFramework/VideoDriverFactory.h @@ -28,6 +28,8 @@ class VideoDriverFactory: public DriverFactory void Load(); private: + friend class StaticVideoDriverFactory; + VideoDriverFactory (); ~VideoDriverFactory (); }; @@ -41,7 +43,8 @@ class VideoDriverRegistor VideoDriverFactory* factory; factory = VideoDriverFactory::GetVideoDriverFactory (); - factory->AddDriver (mDriver); + if (factory) + factory->AddDriver (mDriver); } ~VideoDriverRegistor() @@ -49,7 +52,8 @@ class VideoDriverRegistor VideoDriverFactory* factory; factory = VideoDriverFactory::GetVideoDriverFactory (); - factory->RemoveDriver (mDriver); + if (factory) + factory->RemoveDriver (mDriver); } private: -- cgit v1.2.3