Skip to content

Commit

Permalink
Merge pull request #220 from steinbergmedia/fix_global_font_leaks
Browse files Browse the repository at this point in the history
init and terminate global fonts in a predicated way
  • Loading branch information
scheffle committed Aug 7, 2021
2 parents 07f1b85 + 07d7e70 commit 2e4cb2b
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 66 deletions.
152 changes: 95 additions & 57 deletions vstgui/lib/cfont.cpp
Expand Up @@ -12,57 +12,105 @@ namespace VSTGUI {
//-----------------------------------------------------------------------------
// Global Fonts
//-----------------------------------------------------------------------------
struct GlobalFonts
{
SharedPointer<CFontDesc> systemFont;
SharedPointer<CFontDesc> normalFontVeryBig;
SharedPointer<CFontDesc> normalFontBig;
SharedPointer<CFontDesc> normalFont;
SharedPointer<CFontDesc> normalFontSmall;
SharedPointer<CFontDesc> normalFontSmaller;
SharedPointer<CFontDesc> normalFontVerySmall;
SharedPointer<CFontDesc> symbolFont;
};
static GlobalFonts globalFonts;

//-----------------------------------------------------------------------------
CFontRef kSystemFont = nullptr;
CFontRef kNormalFontVeryBig = nullptr;
CFontRef kNormalFontBig = nullptr;
CFontRef kNormalFont = nullptr;
CFontRef kNormalFontSmall = nullptr;
CFontRef kNormalFontSmaller = nullptr;
CFontRef kNormalFontVerySmall = nullptr;
CFontRef kSymbolFont = nullptr;

//-----------------------------------------------------------------------------
void CFontDesc::init ()
{
#if MAC
#if TARGET_OS_IPHONE
static CFontDesc gSystemFont ("Helvetica", 12);
static CFontDesc gNormalFontVeryBig ("ArialMT", 18);
static CFontDesc gNormalFontBig ("ArialMT", 14);
static CFontDesc gNormalFont ("ArialMT", 12);
static CFontDesc gNormalFontSmall ("ArialMT", 11);
static CFontDesc gNormalFontSmaller ("ArialMT", 10);
static CFontDesc gNormalFontVerySmall ("ArialMT", 9);
static CFontDesc gSymbolFont ("Symbol", 12);
#else
static CFontDesc gSystemFont ("Lucida Grande", 12);
static CFontDesc gNormalFontVeryBig ("Arial", 18);
static CFontDesc gNormalFontBig ("Arial", 14);
static CFontDesc gNormalFont ("Arial", 12);
static CFontDesc gNormalFontSmall ("Arial", 11);
static CFontDesc gNormalFontSmaller ("Arial", 10);
static CFontDesc gNormalFontVerySmall ("Arial", 9);
static CFontDesc gSymbolFont ("Symbol", 12);
#endif
#if TARGET_OS_IPHONE
globalFonts.systemFont = makeOwned<CFontDesc> ("Helvetica", 12);
globalFonts.NormalFontVeryBig = makeOwned<CFontDesc> ("ArialMT", 18);
globalFonts.NormalFontBig = makeOwned<CFontDesc> ("ArialMT", 14);
globalFonts.NormalFont = makeOwned<CFontDesc> ("ArialMT", 12);
globalFonts.NormalFontSmall = makeOwned<CFontDesc> ("ArialMT", 11);
globalFonts.NormalFontSmaller = makeOwned<CFontDesc> ("ArialMT", 10);
globalFonts.NormalFontVerySmall = makeOwned<CFontDesc> ("ArialMT", 9);
globalFonts.SymbolFont = makeOwned<CFontDesc> ("Symbol", 12);
#else
globalFonts.systemFont = makeOwned<CFontDesc> ("Lucida Grande", 12);
globalFonts.normalFontVeryBig = makeOwned<CFontDesc> ("Arial", 18);
globalFonts.normalFontBig = makeOwned<CFontDesc> ("Arial", 14);
globalFonts.normalFont = makeOwned<CFontDesc> ("Arial", 12);
globalFonts.normalFontSmall = makeOwned<CFontDesc> ("Arial", 11);
globalFonts.normalFontSmaller = makeOwned<CFontDesc> ("Arial", 10);
globalFonts.normalFontVerySmall = makeOwned<CFontDesc> ("Arial", 9);
globalFonts.symbolFont = makeOwned<CFontDesc> ("Symbol", 12);
#endif

#elif WINDOWS
static CFontDesc gSystemFont ("Arial", 12);
static CFontDesc gNormalFontVeryBig ("Arial", 18);
static CFontDesc gNormalFontBig ("Arial", 14);
static CFontDesc gNormalFont ("Arial", 12);
static CFontDesc gNormalFontSmall ("Arial", 11);
static CFontDesc gNormalFontSmaller ("Arial", 10);
static CFontDesc gNormalFontVerySmall ("Arial", 9);
static CFontDesc gSymbolFont ("Symbol", 13);
globalFonts.systemFont = makeOwned<CFontDesc> ("Arial", 12);
globalFonts.normalFontVeryBig = makeOwned<CFontDesc> ("Arial", 18);
globalFonts.normalFontBig = makeOwned<CFontDesc> ("Arial", 14);
globalFonts.normalFont = makeOwned<CFontDesc> ("Arial", 12);
globalFonts.normalFontSmall = makeOwned<CFontDesc> ("Arial", 11);
globalFonts.normalFontSmaller = makeOwned<CFontDesc> ("Arial", 10);
globalFonts.normalFontVerySmall = makeOwned<CFontDesc> ("Arial", 9);
globalFonts.symbolFont = makeOwned<CFontDesc> ("Symbol", 13);

#else
static CFontDesc gSystemFont ("Arial", 12);
static CFontDesc gNormalFontVeryBig ("Arial", 18);
static CFontDesc gNormalFontBig ("Arial", 14);
static CFontDesc gNormalFont ("Arial", 12);
static CFontDesc gNormalFontSmall ("Arial", 11);
static CFontDesc gNormalFontSmaller ("Arial", 10);
static CFontDesc gNormalFontVerySmall ("Arial", 9);
static CFontDesc gSymbolFont ("Symbol", 13);
globalFonts.systemFont = makeOwned<CFontDesc> ("Arial", 12);
globalFonts.normalFontVeryBig = makeOwned<CFontDesc> ("Arial", 18);
globalFonts.normalFontBig = makeOwned<CFontDesc> ("Arial", 14);
globalFonts.normalFont = makeOwned<CFontDesc> ("Arial", 12);
globalFonts.normalFontSmall = makeOwned<CFontDesc> ("Arial", 11);
globalFonts.normalFontSmaller = makeOwned<CFontDesc> ("Arial", 10);
globalFonts.normalFontVerySmall = makeOwned<CFontDesc> ("Arial", 9);
globalFonts.symbolFont = makeOwned<CFontDesc> ("Symbol", 13);

#endif
kSystemFont = globalFonts.systemFont;
kNormalFontVeryBig = globalFonts.normalFontVeryBig;
kNormalFontBig = globalFonts.normalFontBig;
kNormalFont = globalFonts.normalFont;
kNormalFontSmall = globalFonts.normalFontSmall;
kNormalFontSmaller = globalFonts.normalFontSmaller;
kNormalFontVerySmall = globalFonts.normalFontVerySmall;
kSymbolFont = globalFonts.symbolFont;
}

const CFontRef kSystemFont = &gSystemFont;
const CFontRef kNormalFontVeryBig = &gNormalFontVeryBig;
const CFontRef kNormalFontBig = &gNormalFontBig;
const CFontRef kNormalFont = &gNormalFont;
const CFontRef kNormalFontSmall = &gNormalFontSmall;
const CFontRef kNormalFontSmaller = &gNormalFontSmaller;
const CFontRef kNormalFontVerySmall = &gNormalFontVerySmall;
const CFontRef kSymbolFont = &gSymbolFont;
//-----------------------------------------------------------------------------
void CFontDesc::cleanup ()
{
globalFonts.systemFont = nullptr;
globalFonts.normalFontVeryBig = nullptr;
globalFonts.normalFontBig = nullptr;
globalFonts.normalFont = nullptr;
globalFonts.normalFontSmall = nullptr;
globalFonts.normalFontSmaller = nullptr;
globalFonts.normalFontVerySmall = nullptr;
globalFonts.symbolFont = nullptr;

kSystemFont = nullptr;
kNormalFontVeryBig = nullptr;
kNormalFontBig = nullptr;
kNormalFont = nullptr;
kNormalFontSmall = nullptr;
kNormalFontSmaller = nullptr;
kNormalFontVerySmall = nullptr;
kSymbolFont = nullptr;
}

//-----------------------------------------------------------------------------
// CFontDesc Implementation
Expand Down Expand Up @@ -91,7 +139,10 @@ CFontDesc::CFontDesc (const CFontDesc& font)
}

//------------------------------------------------------------------------
CFontDesc::~CFontDesc () noexcept = default;
CFontDesc::~CFontDesc () noexcept
{
vstgui_assert (getNbReference () == 0, "Always use shared pointers with CFontDesc!");
}

//-----------------------------------------------------------------------------
void CFontDesc::beforeDelete ()
Expand Down Expand Up @@ -167,17 +218,4 @@ bool CFontDesc::operator == (const CFontDesc& f) const
return true;
}

//-----------------------------------------------------------------------------
void CFontDesc::cleanup ()
{
gSystemFont.freePlatformFont ();
gNormalFontVeryBig.freePlatformFont ();
gNormalFontBig.freePlatformFont ();
gNormalFont.freePlatformFont ();
gNormalFontSmall.freePlatformFont ();
gNormalFontSmaller.freePlatformFont ();
gNormalFontVerySmall.freePlatformFont ();
gSymbolFont.freePlatformFont ();
}

} // VSTGUI
19 changes: 10 additions & 9 deletions vstgui/lib/cfont.h
Expand Up @@ -59,7 +59,8 @@ class CFontDesc : public AtomicReferenceCounted
virtual CFontDesc& operator= (const CFontDesc&);
virtual bool operator== (const CFontDesc&) const;
virtual bool operator!= (const CFontDesc& other) const { return !(*this == other);}


static void init ();
static void cleanup ();

protected:
Expand All @@ -75,13 +76,13 @@ class CFontDesc : public AtomicReferenceCounted
//-----------------------------------------------------------------------------
// Global fonts
//-----------------------------------------------------------------------------
extern const CFontRef kSystemFont;
extern const CFontRef kNormalFontVeryBig;
extern const CFontRef kNormalFontBig;
extern const CFontRef kNormalFont;
extern const CFontRef kNormalFontSmall;
extern const CFontRef kNormalFontSmaller;
extern const CFontRef kNormalFontVerySmall;
extern const CFontRef kSymbolFont;
extern CFontRef kSystemFont;
extern CFontRef kNormalFontVeryBig;
extern CFontRef kNormalFontBig;
extern CFontRef kNormalFont;
extern CFontRef kNormalFontSmall;
extern CFontRef kNormalFontSmaller;
extern CFontRef kNormalFontVerySmall;
extern CFontRef kSymbolFont;

} // VSTGUI
3 changes: 3 additions & 0 deletions vstgui/lib/vstguiinit.cpp
Expand Up @@ -2,6 +2,7 @@
// distribution and at http://github.com/steinbergmedia/vstgui/LICENSE

#include "platform/platformfactory.h"
#include "cfont.h"

//-----------------------------------------------------------------------------
namespace VSTGUI {
Expand All @@ -10,11 +11,13 @@ namespace VSTGUI {
void init (PlatformInstanceHandle instance)
{
initPlatform (instance);
CFontDesc::init ();
}

//-----------------------------------------------------------------------------
void exit ()
{
CFontDesc::cleanup ();
exitPlatform ();
}

Expand Down

0 comments on commit 2e4cb2b

Please sign in to comment.