Memory leak caused by getFont() and WindowsDirectWriteTypeFace

I am going throught the JUCE course made by Outptut and I have come across a memory leak which I can’t find a good way to solve. Basically I have a .h file called “Interface Defines” where I store all the constants used in the painting of the UI. These include 3 juce fonts.

const juce::Font font_1(“Arial”, 12.00f, juce::Font::bold);
const juce::Font font_2(“Arial”, 22.00f, juce::Font::bold);
const juce::Font font_3(“Arial”, 48.00f, juce::Font::bold);

I then use these in a custom LookAndFeelClass:

juce::Font getLabelFont(juce::Label& label) override
{
return font_1;
}

and

juce::Font getTextButtonFont(juce::TextButton&, int buttonHeight) override
{
return font_1;
}

These functions are overriden from LookAndFeelV4. The plugin runs fine until I close it, at which point I get a jassertfalse and an error message which reads

*** Leaked objects detected: 1 instance(s) of class WindowsDirectWriteTypeface

I have tracked down the problem to these two functions. If i declare font_1 as a member of my custom look and feel then it all works fine but I need to be able to access the fonts from other classes. For example in one of my components I call

setFont(font_3)

for a piece of text which also causes the the assertion to trip. I have tried to use std::unique_ptr for the font declarations but the problem persists. I’m wondering if this is just a bug inside juce.

I think the problem stems from the fact these functions dont get references or pointers, they copy the whole object. My question is: is there any way to keep these const Fonts in a seperate file without hitting a memory leak.

I have EXACTLY the same problem. Looking for some suggestions as to how to fix it.
Thanks

Found out how to correct the issue. Add the fonts as private members of the LookAndFeel class:

private:

#ifdef JUCE_MAC
const String fontName = “Helvetica Neue”;
#else
const String fontName = “Arial”;
#endif

const Font font_1 = Font(fontName, 12.00F, Font::bold);
const Font font_2 = Font(fontName, 22.00F, Font::bold);
const Font font_3 = Font(fontName, 48.00F, Font::bold);

Ah I see, thanks, are you perhaps doing the Output JUCE course?

Yes, I am working on the output course. There are still some issues as the final part of the course references the fonts outside of KAPLookAndFeel (in some of the panel classes). It seems like they should be able to be referenced from the lookandfeel class. I have not figured out how yet.

Steven Kraninger