* 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.
This commit is contained in:
harukalover 2009-05-18 05:51:37 +00:00
parent 392fbdfa4d
commit 6f0c5e6489
6 changed files with 37 additions and 35 deletions

View file

@ -44,11 +44,6 @@
#include "utils.h" #include "utils.h"
///////////////////
// Static instance
OpenGLText* OpenGLText::instance;
/////////////// ///////////////
// Constructor // Constructor
OpenGLText::OpenGLText() { OpenGLText::OpenGLText() {
@ -74,9 +69,8 @@ void OpenGLText::Reset() {
//////////////// ////////////////
// Get instance // Get instance
OpenGLText* OpenGLText::GetInstance() { OpenGLText& OpenGLText::GetInstance() {
if (!instance) instance = new OpenGLText(); static OpenGLText instance;
wxASSERT(instance);
return 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 // Get glyph metrics
wxBitmap *OpenGLTextGlyph::tempBmp = NULL; wxBitmap *OpenGLTextGlyph::tempBmp = NULL;

View file

@ -63,6 +63,8 @@ public:
void GetMetrics(); void GetMetrics();
void Draw(int x,int y); void Draw(int x,int y);
~OpenGLTextGlyph();
}; };
typedef std::map<int,OpenGLTextGlyph> glyphMap; typedef std::map<int,OpenGLTextGlyph> glyphMap;
@ -99,19 +101,19 @@ private:
wxString fontFace; wxString fontFace;
wxFont font; wxFont font;
static OpenGLText* instance;
glyphMap glyphs; glyphMap glyphs;
std::vector <OpenGLTextTexture*> textures; std::vector <OpenGLTextTexture*> textures;
OpenGLText(); OpenGLText();
~OpenGLText(); ~OpenGLText();
OpenGLText(OpenGLText const&);
OpenGLText& operator=(OpenGLText const&);
OpenGLTextGlyph GetGlyph(int i); OpenGLTextGlyph GetGlyph(int i);
OpenGLTextGlyph CreateGlyph(int i); OpenGLTextGlyph CreateGlyph(int i);
void Reset(); void Reset();
static OpenGLText* GetInstance(); static OpenGLText& GetInstance();
void DoSetFont(wxString face,int size,bool bold,bool italics); void DoSetFont(wxString face,int size,bool bold,bool italics);
void DoSetColour(wxColour col,float alpha); void DoSetColour(wxColour col,float alpha);
void DoPrint(wxString text,int x,int y); void DoPrint(wxString text,int x,int y);
@ -119,9 +121,9 @@ private:
void DoGetExtent(wxString text,int &w,int &h); void DoGetExtent(wxString text,int &w,int &h);
public: public:
static wxFont GetFont() { return GetInstance()->font; } 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 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 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 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 void GetExtent(wxString text,int &w,int &h) { GetInstance().DoGetExtent(text,w,h); }
}; };

View file

@ -45,8 +45,8 @@
//////////////// ////////////////
// Get instance // Get instance
StandardPaths *StandardPaths::GetInstance() { StandardPaths &StandardPaths::GetInstance() {
if (!instance) instance = new StandardPaths(); static StandardPaths instance;
return instance; return instance;
} }
@ -130,8 +130,3 @@ wxString StandardPaths::DoEncodePath(wxString path) {
void StandardPaths::DoSetPathValue(wxString path,wxString value) { void StandardPaths::DoSetPathValue(wxString path,wxString value) {
paths[path] = value; paths[path] = value;
} }
///////////////////
// Static instance
StandardPaths *StandardPaths::instance = NULL;

View file

@ -47,19 +47,20 @@
// Standard path conversion class // Standard path conversion class
class StandardPaths { class StandardPaths {
private: private:
static StandardPaths *instance; static StandardPaths &GetInstance();
static StandardPaths *GetInstance();
std::map<wxString,wxString> paths; std::map<wxString,wxString> paths;
StandardPaths(); StandardPaths();
StandardPaths(StandardPaths const&);
StandardPaths& operator=(StandardPaths const&);
wxString DoDecodePath(wxString path); wxString DoDecodePath(wxString path);
wxString DoEncodePath(wxString path); wxString DoEncodePath(wxString path);
void DoSetPathValue(wxString path,wxString value); void DoSetPathValue(wxString path,wxString value);
public: public:
static wxString DecodePath(wxString path) { return GetInstance()->DoDecodePath(path); } static wxString DecodePath(wxString path) { return GetInstance().DoDecodePath(path); }
static wxString EncodePath(wxString path) { return GetInstance()->DoEncodePath(path); } static wxString EncodePath(wxString path) { return GetInstance().DoEncodePath(path); }
static void SetPathValue(wxString path,wxString value) { GetInstance()->DoSetPathValue(path,value); } static void SetPathValue(wxString path,wxString value) { GetInstance().DoSetPathValue(path,value); }
}; };

View file

@ -84,9 +84,8 @@ void ToolTipManager::Bind(wxWindow *window,wxString tooltip,wxString hotkey1,wxS
/////////////////// ///////////////////
// Static instance // Static instance
ToolTipManager *ToolTipManager::instance = NULL; ToolTipManager &ToolTipManager::GetInstance() {
ToolTipManager *ToolTipManager::GetInstance() { static ToolTipManager instance;
if (!instance) instance = new ToolTipManager;
return instance; return instance;
} }

View file

@ -63,8 +63,11 @@ private:
// Tooltip manager singleton // Tooltip manager singleton
class ToolTipManager { class ToolTipManager {
private: private:
static ToolTipManager *instance; ToolTipManager() {};
static ToolTipManager *GetInstance(); ToolTipManager(ToolTipManager const&);
ToolTipManager& operator=(ToolTipManager const&);
static ToolTipManager &GetInstance();
std::list<ToolTipBinding> tips; std::list<ToolTipBinding> tips;
@ -72,8 +75,8 @@ private:
void AddTips(wxWindow *window,wxString tooltip,wxArrayString hotkeys); void AddTips(wxWindow *window,wxString tooltip,wxArrayString hotkeys);
public: public:
static void Update() { GetInstance()->DoUpdate(); } 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,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 hotkey=_T(""));
static void Bind(wxWindow *window,wxString tooltip,wxString hotkey1,wxString hotkey2); static void Bind(wxWindow *window,wxString tooltip,wxString hotkey1,wxString hotkey2);
}; };