From bace72cf296c8525a486294e95f8f295276cca3f Mon Sep 17 00:00:00 2001 From: harukalover Date: Mon, 18 May 2009 05:51:37 +0000 Subject: [PATCH] * Fixed some more memory leaks reported by valgrind and msvc by reimplementing a couple of singleton pattern based classes * Fixed a memory leak that occurred from never deleting a wxBitmap allocated in OpenGLTextGlyph Originally committed to SVN as r2952. --- aegisub/src/gl_text.cpp | 18 ++++++++++-------- aegisub/src/gl_text.h | 18 ++++++++++-------- aegisub/src/standard_paths.cpp | 9 ++------- aegisub/src/standard_paths.h | 11 ++++++----- aegisub/src/tooltip_manager.cpp | 5 ++--- aegisub/src/tooltip_manager.h | 11 +++++++---- 6 files changed, 37 insertions(+), 35 deletions(-) diff --git a/aegisub/src/gl_text.cpp b/aegisub/src/gl_text.cpp index a94cdd8e0..22575ed54 100644 --- a/aegisub/src/gl_text.cpp +++ b/aegisub/src/gl_text.cpp @@ -44,11 +44,6 @@ #include "utils.h" -/////////////////// -// Static instance -OpenGLText* OpenGLText::instance; - - /////////////// // Constructor OpenGLText::OpenGLText() { @@ -74,9 +69,8 @@ void OpenGLText::Reset() { //////////////// // Get instance -OpenGLText* OpenGLText::GetInstance() { - if (!instance) instance = new OpenGLText(); - wxASSERT(instance); +OpenGLText& OpenGLText::GetInstance() { + static OpenGLText instance; return instance; } @@ -392,6 +386,14 @@ void OpenGLTextGlyph::Draw(int x,int y) { } +//////////////////// +// Glyph Destructor +OpenGLTextGlyph::~OpenGLTextGlyph() { + if (tempBmp) delete tempBmp; + tempBmp = NULL; +} + + ///////////////////// // Get glyph metrics wxBitmap *OpenGLTextGlyph::tempBmp = NULL; diff --git a/aegisub/src/gl_text.h b/aegisub/src/gl_text.h index 040f2185a..c6de8dc4b 100644 --- a/aegisub/src/gl_text.h +++ b/aegisub/src/gl_text.h @@ -63,6 +63,8 @@ public: void GetMetrics(); void Draw(int x,int y); + + ~OpenGLTextGlyph(); }; typedef std::map glyphMap; @@ -99,19 +101,19 @@ private: wxString fontFace; wxFont font; - static OpenGLText* instance; - glyphMap glyphs; std::vector textures; OpenGLText(); ~OpenGLText(); + OpenGLText(OpenGLText const&); + OpenGLText& operator=(OpenGLText const&); OpenGLTextGlyph GetGlyph(int i); OpenGLTextGlyph CreateGlyph(int i); void Reset(); - static OpenGLText* GetInstance(); + static OpenGLText& GetInstance(); void DoSetFont(wxString face,int size,bool bold,bool italics); void DoSetColour(wxColour col,float alpha); void DoPrint(wxString text,int x,int y); @@ -119,9 +121,9 @@ private: void DoGetExtent(wxString text,int &w,int &h); public: - static wxFont GetFont() { return GetInstance()->font; } - static void SetFont(wxString face=_T("Verdana"),int size=10,bool bold=true,bool italics=false) { GetInstance()->DoSetFont(face,size,bold,italics); } - static void SetColour(wxColour col,float alpha=1.0f) { GetInstance()->DoSetColour(col,alpha); } - static void Print(wxString text,int x,int y) { GetInstance()->DoPrint(text,x,y); } - static void GetExtent(wxString text,int &w,int &h) { GetInstance()->DoGetExtent(text,w,h); } + static wxFont GetFont() { return GetInstance().font; } + static void SetFont(wxString face=_T("Verdana"),int size=10,bool bold=true,bool italics=false) { GetInstance().DoSetFont(face,size,bold,italics); } + static void SetColour(wxColour col,float alpha=1.0f) { GetInstance().DoSetColour(col,alpha); } + static void Print(wxString text,int x,int y) { GetInstance().DoPrint(text,x,y); } + static void GetExtent(wxString text,int &w,int &h) { GetInstance().DoGetExtent(text,w,h); } }; diff --git a/aegisub/src/standard_paths.cpp b/aegisub/src/standard_paths.cpp index 4dff2858a..934edf7c1 100644 --- a/aegisub/src/standard_paths.cpp +++ b/aegisub/src/standard_paths.cpp @@ -45,8 +45,8 @@ //////////////// // Get instance -StandardPaths *StandardPaths::GetInstance() { - if (!instance) instance = new StandardPaths(); +StandardPaths &StandardPaths::GetInstance() { + static StandardPaths instance; return instance; } @@ -130,8 +130,3 @@ wxString StandardPaths::DoEncodePath(wxString path) { void StandardPaths::DoSetPathValue(wxString path,wxString value) { paths[path] = value; } - - -/////////////////// -// Static instance -StandardPaths *StandardPaths::instance = NULL; diff --git a/aegisub/src/standard_paths.h b/aegisub/src/standard_paths.h index 3429d2dfe..8438c154d 100644 --- a/aegisub/src/standard_paths.h +++ b/aegisub/src/standard_paths.h @@ -47,19 +47,20 @@ // Standard path conversion class class StandardPaths { private: - static StandardPaths *instance; - static StandardPaths *GetInstance(); + static StandardPaths &GetInstance(); std::map paths; StandardPaths(); + StandardPaths(StandardPaths const&); + StandardPaths& operator=(StandardPaths const&); wxString DoDecodePath(wxString path); wxString DoEncodePath(wxString path); void DoSetPathValue(wxString path,wxString value); public: - static wxString DecodePath(wxString path) { return GetInstance()->DoDecodePath(path); } - static wxString EncodePath(wxString path) { return GetInstance()->DoEncodePath(path); } - static void SetPathValue(wxString path,wxString value) { GetInstance()->DoSetPathValue(path,value); } + static wxString DecodePath(wxString path) { return GetInstance().DoDecodePath(path); } + static wxString EncodePath(wxString path) { return GetInstance().DoEncodePath(path); } + static void SetPathValue(wxString path,wxString value) { GetInstance().DoSetPathValue(path,value); } }; diff --git a/aegisub/src/tooltip_manager.cpp b/aegisub/src/tooltip_manager.cpp index c66d76b97..de810767d 100644 --- a/aegisub/src/tooltip_manager.cpp +++ b/aegisub/src/tooltip_manager.cpp @@ -84,9 +84,8 @@ void ToolTipManager::Bind(wxWindow *window,wxString tooltip,wxString hotkey1,wxS /////////////////// // Static instance -ToolTipManager *ToolTipManager::instance = NULL; -ToolTipManager *ToolTipManager::GetInstance() { - if (!instance) instance = new ToolTipManager; +ToolTipManager &ToolTipManager::GetInstance() { + static ToolTipManager instance; return instance; } diff --git a/aegisub/src/tooltip_manager.h b/aegisub/src/tooltip_manager.h index 4894937f9..9f2e3596e 100644 --- a/aegisub/src/tooltip_manager.h +++ b/aegisub/src/tooltip_manager.h @@ -63,8 +63,11 @@ private: // Tooltip manager singleton class ToolTipManager { private: - static ToolTipManager *instance; - static ToolTipManager *GetInstance(); + ToolTipManager() {}; + ToolTipManager(ToolTipManager const&); + ToolTipManager& operator=(ToolTipManager const&); + + static ToolTipManager &GetInstance(); std::list tips; @@ -72,8 +75,8 @@ private: void AddTips(wxWindow *window,wxString tooltip,wxArrayString hotkeys); public: - static void Update() { GetInstance()->DoUpdate(); } - static void Bind(wxWindow *window,wxString tooltip,wxArrayString hotkeys) { GetInstance()->AddTips(window,tooltip,hotkeys); } + static void Update() { GetInstance().DoUpdate(); } + static void Bind(wxWindow *window,wxString tooltip,wxArrayString hotkeys) { GetInstance().AddTips(window,tooltip,hotkeys); } static void Bind(wxWindow *window,wxString tooltip,wxString hotkey=_T("")); static void Bind(wxWindow *window,wxString tooltip,wxString hotkey1,wxString hotkey2); };