I’m getting some weird behavior with global const Font variables for JUCE 2.0.21 using Windows Vista.
I have, in my code, a header file of globals with a bunch of const Font’s with names like FONT_BOLD_STANDARD, which I then sometimes pass into various Font-related functions involving Components - the idea being that if I want to then change one of the fonts in my program, I just change what the const Font definition is set to and then the rest of it changes as well.
If I do this, however, I end up getting weird leak detections on exit, with LeakedObjectDetector firing three asserts and telling me that there’s one leaked instance of WindowsDirectWriteTypeface, one leaked instance of Typeface, and one leaked instance of AffineTransform. After this, the problem gets worse - it ends up trying to read some bogus memory in what appears to be ComSmartPtr::release(), which seems to be called as part of the ~WindowsDirectWriteTypeface() destructor, and then crashes completely.
The below code snippet, edited from the Hello World program, reproduces the problem on the latest 2.0.21 JUCE revision on Windows Vista when you hit the “X” button to close the window. The carnage happens in the top class “FoobarButton” - the rest are just generic classes stolen from the demo to actually produce a working program. However, they’re relevant to the bug, since the problem doesn’t seem to occur unless the button is actually displayed.
[code]#include “…/JuceLibraryCode/JuceHeader.h”
const Font f(“Arial”, 15, Font::bold);
class FoobarButton : public TextButton {
public:
FoobarButton():TextButton(“Foobar”) {}
Font getFont() { return f; } //<-- the addition of this line causes the program to crash on close. returning Font(f) doesn’t work either
};
class MainComponent : public Component {
public:
FoobarButton s;
MainComponent () { addAndMakeVisible (&s); setSize (600, 300); }
void resized() { s.setBounds (0,0,getWidth(),getHeight()); }
};
class HelloWorldWindow : public DocumentWindow {
public:
HelloWorldWindow() : DocumentWindow (“JUCE Hello World!”, Colours::lightgrey, DocumentWindow::allButtons, true) {
setContentOwned (new MainComponent(), true);
centreWithSize (getWidth(), getHeight());
setVisible (true);
}
void closeButtonPressed() { JUCEApplication::quit(); }
};
class JUCEHelloWorldApplication : public JUCEApplication {
public:
JUCEHelloWorldApplication() {}
~JUCEHelloWorldApplication() {}
void initialise (const String& commandLine) { helloWorldWindow = new HelloWorldWindow(); }
void shutdown() { delete helloWorldWindow; }
const String getApplicationName() { return "Hello World for JUCE"; }
const String getApplicationVersion() { return ProjectInfo::versionString; }
private:
HelloWorldWindow* helloWorldWindow;
};
START_JUCE_APPLICATION (JUCEHelloWorldApplication)[/code]
Hope that’s enough to describe the problem and that I’ve followed the bug reporting standards properly.