# Font and MessageManager::Lock clarification

**URL:** <https://forum.juce.com/t/font-and-messagemanager-lock-clarification/32349>\
**Category:** General JUCE discussion\
**Created:** [March 19, 2019, 9:25am UTC](https://forum.juce.com/t/font-and-messagemanager-lock-clarification/32349 "2019-03-19T09:25:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexsmode](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@alexsmode](https://forum.juce.com/u/alexsmode)\
**Post date:** [March 19, 2019, 9:25am UTC](https://forum.juce.com/t/font-and-messagemanager-lock-clarification/32349/1 "2019-03-19T09:25:01Z")

</div>

Hi !

I just update juce\_graphics module to Juce 5 developer branch.  
And I find the “new” clever assert in Font::getStringWidthFloat and Font::getGlyphPositions:

```
// This call isn't thread-safe when there's a message thread running
jassert (MessageManager::getInstanceWithoutCreating() == nullptr
           || MessageManager::getInstanceWithoutCreating()->currentThreadHasLockedMessageManager());

```

I’m wondering why this is not thread safe ?  
Is it because of ` ReferenceCountedObjectPtr<SharedFontInternal> Font::font;`  
or because of `TypefaceCache::getInstance()` ?

If it is related to TypefaceCache, may be we should move this assert in `Font::getTypeface()`  
method instead because there is other extra callers that should be protected:  
`Font::getAscent() Font::getAvailableStyles() Font::getHeightToPointsFactor()`

I’m looking at this code because I made some offscreen Juce::Image Font rendering in my OpenGL thread, so I protect my caller code with:

```
juce::MessageManager::Lock managerLock;
managerLock.enter();
... // text rendering
managerLock.exit();

```

Hoping that client of my code already start the dispatch loop. (I do not know the way to check it, help is welcome here)

Thanks in advance for your advices.

NB: I already have related issue 3 years before with Juce 4 here:

> [@Threading issue with Graphics::drawText](https://forum.juce.com/t/threading-issue-with-graphics-drawtext/16520):
>
> Hi! I think I found a threading issue with Graphics::drawText My application have two threads: The main thread displaying a lots of juce::Component The second thread starting early at application initialization, does internal computation without display anything. This second thread does some off-screen text rendering on local juce::Image like this: static juce::Image createImageFromText(const String& text) { juce::Image image(juce::Image::ARGB, 320, 180, false); juce::Graphics g(…

All the bests

---

<div class="post-metadata">

**Author:** ![alexsmode](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@alexsmode](https://forum.juce.com/u/alexsmode)\
**Post date:** [April 19, 2019, 6:16am UTC](https://forum.juce.com/t/font-and-messagemanager-lock-clarification/32349/2 "2019-04-19T06:16:16Z")

</div>

Hi  
Nobody can enlighten me on this?

Best regards

---

<div class="post-metadata">

**Author:** ![ed95](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ed95/32/7635_2.png) [@ed95](https://forum.juce.com/u/ed95)\
**Post date:** [April 23, 2019, 11:24am UTC](https://forum.juce.com/t/font-and-messagemanager-lock-clarification/32349/3 "2019-04-23T11:24:38Z")

</div>

It was added [here](https://forum.juce.com/t/font-getstringwidth-on-non-message-thread/20853) to guard against race conditions that can occur when calling the underlying native functions from different threads. Calling `TypefaceCache::getInstance()` or using `ReferenceCountedObjectPtr<SharedFontInternal>` isn’t the issue here as I think the JUCE graphics code is mostly thread-safe, but obviously there’s nothing we can do if the native methods that are eventually called aren’t.

It might be worth reading through these related threads to see if they can help:

> [@Font::getStringWidth on non-message thread](https://forum.juce.com/t/font-getstringwidth-on-non-message-thread/20853):
>
> I encountered a crash in WindowsTypeface::getOutlineForGlyph when a text is drawn and at the same time another thread is querying a string width using Font::getStringWidth. This is using an older version of JUCE (3.0.5); I’m not sure whether this is relevant for this issue, the according code in juce\_win32\_fonts.cpp doesn’t seem to have changed much. I’m also rather sure that I only experienced the crash on Windows. From what I see, the crash is due to some stuff in Font::getStringWidth that i…

> [@Graphics::setFont and MessageManager Locking](https://forum.juce.com/t/graphics-setfont-and-messagemanager-locking/24657):
>
> Dear Jules, Roli Team and Community, I have updated Juce library to the last commit and I observed there is a jassert done in the c786c commit. Such (two) jassert are in juce\_font.cpp: void Font::getGlyphPositions (const String& text, Array& glyphs, Array& xOffsets) const { // This call isn’t thread-safe when there’s a message thread running jassert (MessageManager::getInstanceWithoutCreating() == nullptr || MessageManager::getInstanceWithoutCreating()-\>currentThreadHasLockedMessageMana…

---

<div class="post-metadata">

**Author:** ![alexsmode](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@alexsmode](https://forum.juce.com/u/alexsmode)\
**Post date:** [April 24, 2019, 10:15am UTC](https://forum.juce.com/t/font-and-messagemanager-lock-clarification/32349/4 "2019-04-24T10:15:40Z")

</div>

Hi Ed and thanks for reply.

I review the forum posts and `TypefaceCache` code and everything is protected by a ReadWriteLock, so indeed, I do not think theire is any thread safety issue here.

`ReferenceCountedObjectPtr<SharedFontInternal> Font::font;` use atomic ReferenceCountedObject so no thread safety neither.

Everything seems perfectly elegant and fonctionning very well.  
So indeed, those assert are usefull only in the case of non thread safe native methods.

Many thanks for this highlight.

All the best

---

<div class="post-metadata">

**Author:** ![OBO](https://avatars.discourse-cdn.com/v4/letter/o/ba8739/32.png) [@OBO](https://forum.juce.com/u/OBO)\
**Post date:** [April 24, 2019, 11:29am UTC](https://forum.juce.com/t/font-and-messagemanager-lock-clarification/32349/5 "2019-04-24T11:29:01Z")

</div>

In the case the font is a custom typeface, it will not call into the native stuff, so the Assert is not needed in this case.
